Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/github_actions/actions/checkout-4
Browse files Browse the repository at this point in the history
  • Loading branch information
tcort authored May 21, 2024
2 parents 072a7ee + 5387de4 commit 37b07e1
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 304 deletions.
21 changes: 0 additions & 21 deletions .eslintrc.js

This file was deleted.

8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ on:

jobs:
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-latest
node-version:
- 18
- 20
Expand All @@ -22,7 +26,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changes

## Version 3.12
## Version 3.12.1

- fix: fix crash #297 @CanadaHonk
- Set `ipv4first` for tests in CI @dklimpel
- Bump node for release @dklimpel

## Version 3.12.0

- feat: add basic ignore subpaths argument @CanadaHonk
- feat: add global option to replacementPatterns @CanadaHonk
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Test library workflow status](https://github.com/tcort/markdown-link-check/workflows/Test%20library/badge.svg)
![Test library workflow status](https://github.com/tcort/markdown-link-check/actions/workflows/ci.yml/badge.svg)

# markdown-link-check

Expand Down Expand Up @@ -50,6 +50,21 @@ To run as a [pre-commit hook](https://pre-commit.com):
args: [-q]
```

## Run in a GitLab pipeline

```yaml
linkchecker:
stage: test
image:
name: ghcr.io/tcort/markdown-link-check:3.11.2
entrypoint: ["/bin/sh", "-c"]
script:
- find . -name \*.md -print0 | xargs -0 -n1 markdown-link-check
rules:
- changes:
- "**/*.md"
```
## Run in other tools
- [Mega-Linter](https://megalinter.io/latest/): Linters aggregator [including markdown-link-check](https://megalinter.io/latest/descriptors/markdown_markdown_link_check/)
Expand Down
26 changes: 26 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import js from "@eslint/js";
import globals from "globals";

export default [
js.configs.recommended,
{
languageOptions: {
globals: {
...globals.node,
},
parserOptions: {
ecmaVersion: "latest",
moduleType: "commonjs"
},
},
},
{
files: ["test/*.js"],
languageOptions: {
globals: {
...globals.node,
...globals.mocha,
},
},
},
];
42 changes: 41 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ function performSpecialReplacements(str, opts) {
return str;
}

function extractSections(markdown) {
// First remove code blocks.
markdown = markdown.replace(/^```[\S\s]+?^```$/mg, '');

const sectionTitles = markdown.match(/^#+ .*$/gm) || [];

const sections = sectionTitles.map(section =>
section.replace(/^\W+/, '').replace(/\W+$/, '').replace(/[^\w\s-]+/g, '').replace(/\s+/g, '-').toLowerCase()
);

var uniq = {};
for (var section of sections) {
if (section in uniq) {
uniq[section]++;
section = section + '-' + uniq[section];
}
uniq[section] = 0;
}
const uniqueSections = Object.keys(uniq) ?? [];

return uniqueSections;
}

module.exports = function markdownLinkCheck(markdown, opts, callback) {
if (arguments.length === 2 && typeof opts === 'function') {
// optional 'opts' not supplied.
Expand All @@ -62,6 +85,7 @@ module.exports = function markdownLinkCheck(markdown, opts, callback) {
}

const links = markdownLinkExtractor(markdown);
const sections = extractSections(markdown);
const linksCollection = _.uniq(links);
const bar = (opts.showProgressBar) ?
new ProgressBar('Checking... [:bar] :percent', {
Expand Down Expand Up @@ -114,8 +138,24 @@ module.exports = function markdownLinkCheck(markdown, opts, callback) {
}
}

linkCheck(link, opts, function (err, result) {
let sectionLink = null;

if (link.startsWith('#')) {
sectionLink = link;
}
else if ('baseUrl' in opts && link.startsWith(opts.baseUrl)) {
if (link.substring(opts.baseUrl.length).match(/^\/*#/)) {
sectionLink = link.replace(/^[^#]+/, '');
}
}

if (sectionLink) {
const result = new LinkCheckResult(opts, sectionLink, sections.includes(sectionLink.substring(1)) ? 200 : 404, undefined);
callback(null, result);
return;
}

linkCheck(link, opts, function (err, result) {
if (opts.showProgressBar) {
bar.tick();
}
Expand Down
15 changes: 13 additions & 2 deletions markdown-link-check
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ function getInputs() {
continue;
}

baseUrl = 'file://' + path.dirname(resolved);
if (process.platform === 'win32') {
baseUrl = 'file://' + path.dirname(resolved).replace(/\\/g, '/');
}
else {
baseUrl = 'file://' + path.dirname(resolved);
}

stream = fs.createReadStream(filenameOrUrl);
}

Expand All @@ -124,7 +130,12 @@ function getInputs() {
input.opts.projectBaseUrl = `file://${program.projectBaseUrl}`;
} else {
// set the default projectBaseUrl to the current working directory, so that `{{BASEURL}}` can be resolved to the project root.
input.opts.projectBaseUrl = `file://${process.cwd()}`;
if (process.platform === 'win32') {
input.opts.projectBaseUrl = `file:///${process.cwd().replace(/\\/g, '/')}`;
}
else {
input.opts.projectBaseUrl = `file://${process.cwd()}`;
}
}
}

Expand Down
Loading

0 comments on commit 37b07e1

Please sign in to comment.