From 94b51b57872757bfded3c97d0a03cc432c59328b Mon Sep 17 00:00:00 2001 From: Strokkur24 Date: Sat, 6 Jun 2026 18:11:28 +0200 Subject: [PATCH] feat: strip MDX imports --- index.js | 4 ++++ test/remove-markdown.js | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/index.js b/index.js index 367cc52..25a822a 100644 --- a/index.js +++ b/index.js @@ -48,6 +48,8 @@ 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'); } @@ -55,6 +57,8 @@ module.exports = function(md, options) { output = output // Remove HTML tags .replace(htmlReplaceRegex, '') + // Remove import statements + .replace(importReplaceRegex, '') // Remove setext-style headers .replace(/^[=\-]{2,}\s*$/g, '') // Remove footnotes? diff --git a/test/remove-markdown.js b/test/remove-markdown.js index 812b749..f2f5584 100644 --- a/test/remove-markdown.js +++ b/test/remove-markdown.js @@ -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.';