Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ module.exports = function(md, options) {
)
}

let importReplaceRegex = /^import\s+([a-zA-Z0-9{}\-_,* /]+from)?\s*["'][^'"]+["'];?$\n+/gm

if (options.separateLinksAndTexts) {
output = output.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1' + options.separateLinksAndTexts + '$2');
}

output = output
// Remove HTML tags
.replace(htmlReplaceRegex, '')
// Remove import statements
.replace(importReplaceRegex, '')
// Remove setext-style headers
.replace(/^[=\-]{2,}\s*$/g, '')
// Remove footnotes?
Expand Down
12 changes: 12 additions & 0 deletions test/remove-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ describe('remove Markdown', function () {
expect(removeMd(string)).to.equal(expected);
});

it('should strip MDX import statements', function () {
const tests = [
{ string: 'import page404 from "@/assets/images/404.png"\n\nHere is some text', expected: 'Here is some text' },
{ string: 'import "mycomponent.astro";\nWelcome back!', expected: 'Welcome back!' },
{ string: "import { Validator as val } from '../util.js'\nSuper imports?", expected: 'Super imports?' },
{ string: 'import page404 from "@/assets/images/404.png";\nimport page403 from "@/assets/images/403.png";\n\nSome errors, huh', expected: 'Some errors, huh' },
];
tests.forEach(function (test) {
expect(removeMd(test.string)).to.equal(test.expected);
});
})

it('should strip anchors', function () {
const string = '*Javascript* [developers](https://engineering.condenast.io/)* are the _best_.';
const expected = 'Javascript developers* are the best.';
Expand Down