From e20ee54b051e7328795700dc9dbd1374df088f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Wed, 31 Jan 2024 00:10:41 +0100 Subject: [PATCH] fix: should only check valid fragments in markdown (.md) files --- src/index.js | 4 ++++ .../only-parse-markdown-files-for-fragments/abc.txt | 0 .../only-parse-markdown-files-for-fragments/awesome.html | 9 +++++++++ .../only-parse-markdown-files-for-fragments.md | 7 +++++++ test/index.test.js | 5 +++++ 5 files changed, 25 insertions(+) create mode 100644 test/fixtures/valid/only-parse-markdown-files-for-fragments/abc.txt create mode 100644 test/fixtures/valid/only-parse-markdown-files-for-fragments/awesome.html create mode 100644 test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md diff --git a/src/index.js b/src/index.js index dc074ef..7daa3df 100644 --- a/src/index.js +++ b/src/index.js @@ -97,6 +97,10 @@ const customRule = { continue } + if (!url.pathname.endsWith(".md")) { + continue + } + const fileContent = fs.readFileSync(url, { encoding: "utf8" }) const headings = getMarkdownHeadings(fileContent) const idOrAnchorNameHTMLFragments = diff --git a/test/fixtures/valid/only-parse-markdown-files-for-fragments/abc.txt b/test/fixtures/valid/only-parse-markdown-files-for-fragments/abc.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/valid/only-parse-markdown-files-for-fragments/awesome.html b/test/fixtures/valid/only-parse-markdown-files-for-fragments/awesome.html new file mode 100644 index 0000000..a0bb4f4 --- /dev/null +++ b/test/fixtures/valid/only-parse-markdown-files-for-fragments/awesome.html @@ -0,0 +1,9 @@ + + + + + + Awesome + + + diff --git a/test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md b/test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md new file mode 100644 index 0000000..7496d74 --- /dev/null +++ b/test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md @@ -0,0 +1,7 @@ +# Valid + +[Link fragment HTML](./awesome.html#existing-heading) + +[Link fragment TXT](./abc.txt#existing-heading) + +[Link fragment Image](../../image.png#existing-heading) diff --git a/test/index.test.js b/test/index.test.js index 451d728..3aa493a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -123,6 +123,11 @@ test("ensure the rule validates correctly", async (t) => { fixturePath: "test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md", }, + { + name: "should only parse markdown files for fragments checking", + fixturePath: + "test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md", + }, { name: "with an existing file", fixturePath: "test/fixtures/valid/existing-file.md",