Skip to content

Commit

Permalink
Merge pull request #58 from UmbrellaDocs/section-links-fix
Browse files Browse the repository at this point in the history
Use GitHub slugger for converting ID to slug
  • Loading branch information
gaurav-nelson authored May 28, 2024
2 parents 7b5fe4d + 1b3aff4 commit 14511bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/check-file-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import { visit } from "unist-util-visit";
import GithubSlugger from 'github-slugger'

/**
* Checks if a file and a section within the file exist.
Expand All @@ -19,6 +20,7 @@ function checkFileExistence(link, file) {
let errorMessage = "";

try {
let slugger = new GithubSlugger()
// Split the URL into the file part and the section part
const [urlWithoutSection = "", sectionId = null] = link.url.split("#");

Expand All @@ -40,14 +42,14 @@ function checkFileExistence(link, file) {
const tree = unified().use(remarkParse).use(remarkGfm).parse(mdContent);

// 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 headingId = node.children[0].type === "html"
? node.children[0].value.match(/name="(.+?)"/)?.[1]
: node.children[0].value.includes("{#")
? node.children[0].value.match(/{#(.+?)}/)?.[1]
: node.children[0].value.toLowerCase().replace(/ /g, "-").replace(/\./g, "");

: slugger.slug(node.children[0].value);
headingNodes.add(headingId);
});

Expand Down
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://github.com/UmbrellaDocs/linkspector#readme",
"dependencies": {
"commander": "^12.0.0",
"github-slugger": "^2.0.0",
"glob": "^10.3.12",
"ignore": "^5.3.1",
"joi": "^17.13.1",
Expand Down

0 comments on commit 14511bd

Please sign in to comment.