Skip to content

Commit

Permalink
feat: Add support for checking HTML encoded section links
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-nelson committed Jun 26, 2024
1 parent 33f0db3 commit 6496fdb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
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');
})
28 changes: 17 additions & 11 deletions lib/check-file-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function checkFileExistence(link, file) {
const filePath = urlWithoutSection.startsWith('/')
? path.join(process.cwd(), urlWithoutSection)
: urlWithoutSection === '' || urlWithoutSection === path.basename(file)
? file
: path.resolve(path.dirname(file), urlWithoutSection)
? file
: path.resolve(path.dirname(file), urlWithoutSection)

// Check if the file exists
if (!fs.existsSync(filePath)) {
Expand All @@ -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] &&
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)
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).

0 comments on commit 6496fdb

Please sign in to comment.