Skip to content

Commit

Permalink
Merge pull request #40 from UmbrellaDocs/section-check-same-file
Browse files Browse the repository at this point in the history
Fix: Check section links in the same file.
  • Loading branch information
gaurav-nelson authored May 8, 2024
2 parents 9dc2d13 + 3b524ed commit 0538423
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
16 changes: 11 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ Thank you for considering contributing to Linkspector! We welcome contributions
- [Reporting Issues](#reporting-issues)
- [Submitting Pull Requests](#submitting-pull-requests)
4. [Development Setup](#development-setup)
5. [Coding Guidelines](#coding-guidelines)
6. [Testing](#testing)
7. [Documentation](#documentation)
8. [Commit Messages](#commit-messages)
9. [License](#license)
5. [Testing](#testing)
6. [Commit Messages](#commit-messages)
7. [License](#license)

## Getting Started

Expand Down Expand Up @@ -50,6 +48,14 @@ Our maintainers will review your PR as soon as possible and provide feedback if

To set up a development environment, follow the instructions in the [Development Setup](DEV_SETUP.md) document. This will guide you through the process of installing dependencies and configuring your development environment.

## Testing

Before submitting a pull request, make sure to run the test suite to ensure that your changes do not introduce any regressions. To run the tests, use the following command:

```bash
npm test
```

## Commit Messages

Follow these guidelines for commit messages:
Expand Down
27 changes: 14 additions & 13 deletions lib/check-file-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,45 @@ function checkFileExistence(link, file) {
let fileDir = path.dirname(file);
let [urlWithoutSection, sectionId] = link.url.split("#");
let filePath = path.resolve(fileDir, urlWithoutSection);

if (link.url.startsWith("#")) {
sectionId = link.url.slice(1);
filePath = file;
}

if (fs.existsSync(filePath)) {
statusCode = "200";
status = "alive";
if (sectionId) {
let mdContent = fs.readFileSync(filePath, "utf8");
const tree = unified().use(remarkParse).use(remarkGfm).parse(mdContent);

let headingNodes = [];
let headingNodes = new Set();
visit(tree, "heading", (node) => {
let headingId;
// Check if the first child is an HTML node
if (node.children[0].type === "html") {
// If it is, extract the id from it
let match = node.children[0].value.match(/name="(.+?)"/);
if (match) {
headingId = match[1];
}
} else {
// If it's not, generate the id from the heading text
let headingText = node.children[0].value;
// Check if the heading text contains a custom id
let match = headingText.match(/{#(.+?)}/);
if (match) {
// If it does, use the custom id
headingId = match[1];
if (headingText.includes("{#")) {
let match = headingText.match(/{#(.+?)}/);
if (match) {
headingId = match[1];
}
} else {
// If it doesn't, generate the id from the heading text
headingId = headingText
.toLowerCase()
.replace(/ /g, "-")
.replace(/\./g, "");
}
}
headingNodes.push(headingId);
headingNodes.add(headingId);
});

// Check if sectionId exists in headingNodes
if (!headingNodes.includes(sectionId)) {
if (!headingNodes.has(sectionId)) {
statusCode = "404";
status = "error";
errorMessage = `Cannot find section: ${sectionId} in file: ${link.url}.`;
Expand Down
1 change: 0 additions & 1 deletion lib/get-unique-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function getUniqueLinks(astNodes) {
(node.type === "link" || node.type === "definition" || node.type === "image") &&
node.url &&
!uniqueUrls.has(node.url) &&
!node.url.startsWith("#") &&
!node.url.startsWith("mailto:")
) {
uniqueUrls.add(node.url);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umbrelladocs/linkspector",
"version": "0.3.3",
"version": "0.3.4",
"description": "Uncover broken links in your content.",
"type": "module",
"main": "linkspector.js",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/relative/relative1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

[Link to Relative 2 Heading Level One](relative2.md#relative-2-heading-level-one)
[Link to Relative 1 Heading Level Two](#relative-1-heading-level-two)

## Relative 1 Heading Level Two

Expand Down

0 comments on commit 0538423

Please sign in to comment.