Skip to content

Commit cdb2caf

Browse files
committed
Re-parse with lint.NewParser for link-ref lookup
hasTOCLinkReference previously used goldmark.New(), which omits mdsmith's PIBlockParserPrioritized. Extract the parser construction in lint.NewFile into an exported lint.NewParser() so rules that need a secondary parse (here, to consult the link reference map for [TOC] suppression) stay consistent with the original lint parse instead of drifting from it. No behavior change on the current test corpus; defensive alignment for future parser additions. Addresses review feedback on PR #144.
1 parent 70b76ad commit cdb2caf

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

internal/lint/file.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ func (f *File) GetGitignore() *GitignoreMatcher {
5555
return f.gitignoreVal
5656
}
5757

58-
// NewFile parses source as Markdown and returns a File.
59-
func NewFile(path string, source []byte) (*File, error) {
60-
reader := text.NewReader(source)
61-
p := parser.NewParser(
58+
// NewParser returns a goldmark parser configured identically to the one
59+
// used by NewFile. Rules that need to re-inspect a document (for example,
60+
// to consult the link reference definition map) should use this so that
61+
// processing-instruction blocks and other mdsmith-specific parsing
62+
// decisions stay consistent with the original lint parse.
63+
func NewParser() parser.Parser {
64+
return parser.NewParser(
6265
parser.WithBlockParsers(
6366
append(parser.DefaultBlockParsers(),
6467
PIBlockParserPrioritized(),
@@ -71,7 +74,12 @@ func NewFile(path string, source []byte) (*File, error) {
7174
parser.DefaultParagraphTransformers()...,
7275
),
7376
)
74-
node := p.Parse(reader)
77+
}
78+
79+
// NewFile parses source as Markdown and returns a File.
80+
func NewFile(path string, source []byte) (*File, error) {
81+
reader := text.NewReader(source)
82+
node := NewParser().Parse(reader)
7583

7684
lines := bytes.Split(source, []byte("\n"))
7785

internal/rules/tocdirective/rule.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/jeduden/mdsmith/internal/lint"
1212
"github.com/jeduden/mdsmith/internal/rule"
13-
"github.com/yuin/goldmark"
1413
"github.com/yuin/goldmark/ast"
1514
"github.com/yuin/goldmark/parser"
1615
"github.com/yuin/goldmark/text"
@@ -120,17 +119,17 @@ func buildMessage(token string) string {
120119
}
121120

122121
// hasTOCLinkReference returns true when the document defines a link
123-
// reference with label "TOC" (CommonMark-normalized). Re-parsing is the
124-
// simplest way to delegate label normalization and code-block scoping to
125-
// goldmark itself, rather than approximating them with a source-level
126-
// scan.
122+
// reference with label "TOC" (CommonMark-normalized). It re-parses with
123+
// lint.NewParser so the parser configuration (including mdsmith's PI
124+
// block parser) matches the original lint parse; otherwise content
125+
// absorbed into a processing-instruction block could register as a link
126+
// reference here while being hidden from the rule's AST walk.
127127
func hasTOCLinkReference(source []byte) bool {
128128
if len(source) == 0 {
129129
return false
130130
}
131-
md := goldmark.New()
132131
ctx := parser.NewContext()
133-
md.Parser().Parse(text.NewReader(source), parser.WithContext(ctx))
132+
lint.NewParser().Parse(text.NewReader(source), parser.WithContext(ctx))
134133
_, ok := ctx.Reference("toc")
135134
return ok
136135
}

0 commit comments

Comments
 (0)