Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Add support for checking HTML encoded section links #70

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
32 changes: 19 additions & 13 deletions lib/check-file-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<a\s+.*?name="(.+?)".*?>/)
if (anchorNameMatch) {
const anchorName = anchorNameMatch[1]
headingNodes.add(anchorName)
}
}
})

// Decode the section ID from the URL
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dirs:
- ./test/fixtures/markdown/with-html-anchors
9 changes: 9 additions & 0 deletions test/fixtures/markdown/with-html-anchors/html-anchor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This is heading 1

This is a paragraph in the first file in a first level heading.

Anchor with `a` <a name="custom-id-level-one"></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).
Loading