diff --git a/index.test.js b/index.test.js
index 1b5de6d..225f7d9 100644
--- a/index.test.js
+++ b/index.test.js
@@ -220,3 +220,37 @@ test('linkspector should check HTML encoded section links', async () => {
expect(results[2].status).toBe('alive')
expect(results[3].status).toBe('alive')
})
+
+test('linkspector should check HTML encoded section links and include anchor names', async () => {
+ let hasErrorLinks = false
+ let currentFile = '' // Variable to store the current file name
+ let results = [] // Array to store the results if json is true
+
+ for await (const { file, result, headings } of linkspector(
+ './test/fixtures/markdown/with-html-anchors/.withHtmlAnchorsTest.yml',
+ cmd
+ )) {
+ currentFile = file
+ for (const linkStatusObj of result) {
+ if (cmd.json) {
+ results.push({
+ file: currentFile,
+ link: linkStatusObj.link,
+ status_code: linkStatusObj.status_code,
+ line_number: linkStatusObj.line_number,
+ position: linkStatusObj.position,
+ status: linkStatusObj.status,
+ error_message: linkStatusObj.error_message,
+ })
+ }
+ if (linkStatusObj.status === 'error') {
+ hasErrorLinks = true
+ }
+ }
+ }
+
+ // Test expectations for link checks
+ expect(hasErrorLinks).toBe(false)
+ expect(results.length).toBe(1)
+ expect(results[0].status).toBe('alive')
+})
diff --git a/lib/check-file-links.js b/lib/check-file-links.js
index 3f27641..25ca07b 100644
--- a/lib/check-file-links.js
+++ b/lib/check-file-links.js
@@ -51,19 +51,25 @@ function checkFileExistence(link, file) {
// Collect all heading IDs in the file
// Use GitHub slugger to generate the heading slug for comparison
const headingNodes = new Set()
- visit(tree, 'heading', (node) => {
- const headingText = getText(node)
-
- const headingId =
- node.children[0].type === 'html'
- ? node.children[0].value.match(/name="(.+?)"/)?.[1]
- : node.children[0] &&
- node.children[0].value &&
- node.children[0].value.includes('{#')
- ? node.children[0].value.match(/{#(.+?)}/)?.[1]
- : slugger.slug(headingText)
-
- headingNodes.add(headingId)
+ visit(tree, ['heading', 'html'], (node) => {
+ if (node.type === 'heading') {
+ const headingText = getText(node)
+ const headingId =
+ node.children[0].type === 'html'
+ ? node.children[0].value.match(/name="(.+?)"/)?.[1]
+ : node.children[0] &&
+ node.children[0].value &&
+ node.children[0].value.includes('{#')
+ ? node.children[0].value.match(/{#(.+?)}/)?.[1]
+ : slugger.slug(headingText)
+ headingNodes.add(headingId)
+ } else if (node.type === 'html') {
+ const anchorNameMatch = node.value.match(//)
+ if (anchorNameMatch) {
+ const anchorName = anchorNameMatch[1]
+ headingNodes.add(anchorName)
+ }
+ }
})
// Decode the section ID from the URL
diff --git a/test/fixtures/markdown/with-html-anchors/.withHtmlAnchorsTest.yml b/test/fixtures/markdown/with-html-anchors/.withHtmlAnchorsTest.yml
new file mode 100644
index 0000000..907d803
--- /dev/null
+++ b/test/fixtures/markdown/with-html-anchors/.withHtmlAnchorsTest.yml
@@ -0,0 +1,2 @@
+dirs:
+ - ./test/fixtures/markdown/with-html-anchors
diff --git a/test/fixtures/markdown/with-html-anchors/html-anchor.md b/test/fixtures/markdown/with-html-anchors/html-anchor.md
new file mode 100644
index 0000000..abf51bc
--- /dev/null
+++ b/test/fixtures/markdown/with-html-anchors/html-anchor.md
@@ -0,0 +1,9 @@
+# This is heading 1
+
+This is a paragraph in the first file in a first level heading.
+
+Anchor with `a`
+
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel mauris sit amet ipsum venenatis placerat.
+
+Link to anchor with `a` [Link to custom id level one](#custom-id-level-one).