Skip to content

Commit 0f1b2c8

Browse files
committed
chore: ignore API reference anchors
1 parent 6c73df6 commit 0f1b2c8

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

packages/doom/src/remark-lint/no-unmatched-anchor.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const noUnmatchedAnchor = lintRule<Root>(
7474
const filepath = vfile.path
7575
const dirpath = path.dirname(filepath)
7676
const configRoot = config.root!
77+
const configLang = config.lang!
7778

7879
const relativePath = path.relative(configRoot, filepath)
7980

@@ -117,43 +118,47 @@ export const noUnmatchedAnchor = lintRule<Root>(
117118

118119
const { url: parsedUrl, hash } = parseUrl(url)
119120

120-
let referencedFilepath = filepath
121+
let refFilepath = filepath
121122

122123
if (parsedUrl.startsWith('/')) {
123-
referencedFilepath = path.resolve(configRoot, config.lang! + parsedUrl)
124+
refFilepath = path.resolve(configRoot, configLang + parsedUrl)
124125
} else if (parsedUrl) {
125-
referencedFilepath = path.resolve(dirpath, parsedUrl)
126+
refFilepath = path.resolve(dirpath, parsedUrl)
126127
}
127128

128-
let ext = path.extname(referencedFilepath)
129+
let ext = path.extname(refFilepath)
129130

130131
if (ext === '.html') {
131-
referencedFilepath = referencedFilepath.slice(0, -ext.length)
132+
refFilepath = refFilepath.slice(0, -ext.length)
132133
ext = ''
133134
}
134135

135136
if (!ext) {
136137
for (const ext of ['.md', '.mdx'] as const) {
137-
if (fs.existsSync(referencedFilepath + ext)) {
138-
referencedFilepath += ext
138+
if (fs.existsSync(refFilepath + ext)) {
139+
refFilepath += ext
139140
break
140141
}
141142
}
142143
}
143144

144-
if (!isDoc(referencedFilepath)) {
145+
if (!isDoc(refFilepath)) {
145146
return
146147
}
147148

148149
// If the referenced file does not exist, we ignore it here and let the `check-dead-links` rule handle it
149-
if (
150-
filepath !== referencedFilepath &&
151-
!fs.existsSync(referencedFilepath)
152-
) {
150+
if (filepath !== refFilepath && !fs.existsSync(refFilepath)) {
151+
return
152+
}
153+
154+
const relativeRefPath = path.relative(configRoot, refFilepath)
155+
156+
// We ignore API docs because their anchors are generated dynamically and may not be present in the source files
157+
if (relativeRefPath.startsWith(`${configLang}/apis/`)) {
153158
return
154159
}
155160

156-
const anchors = getAnchors(referencedFilepath)
161+
const anchors = getAnchors(refFilepath)
157162

158163
if (!anchors.has(hash)) {
159164
vfile.message(
@@ -163,7 +168,7 @@ export const noUnmatchedAnchor = lintRule<Root>(
163168
.map((a) => `\`${a}\``)
164169
.join(', ')}] in file \`${path.relative(
165170
configRoot,
166-
referencedFilepath,
171+
refFilepath,
167172
)}\``,
168173
{ ancestors: [...parents, node], place: node.position },
169174
)

0 commit comments

Comments
 (0)