Skip to content

feat: strip MDX imports#113

Open
Strokkur424 wants to merge 1 commit into
zuchka:mainfrom
Strokkur424:feat/strip-mdx-import-statements
Open

feat: strip MDX imports#113
Strokkur424 wants to merge 1 commit into
zuchka:mainfrom
Strokkur424:feat/strip-mdx-import-statements

Conversation

@Strokkur424

Copy link
Copy Markdown

In extended markdown syntax (MDX), you can have JS-style import statements at the top of the file. Currently, with remove-markdown, these are included in the output, which makes post previews basically useless.

image

This PR changes that by adding a regex that checks for import statements and removes these. The regex ensures that the import statement does not contain leading characters and ends in at least one newline (trailing newlines are removed so the content part starts instantly, as if the newline was never there). This is technically more restrictive than the real limitations of JS-style imports, but seemed to me like a respectable limitation.


The regex is kept very simple and does not check for exact import syntax; it primarily just checks for the import keyword and any combination of the valid characters.


I've added the following test case:

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 checks for a few common variants of import statements.


Let me know if there's anything missing or if I should restrict the regex further. Alternatively Allow edits by maintainers is enabled, so the PR can be edited to fix code smells.

@zuchka

zuchka commented Jun 29, 2026

Copy link
Copy Markdown
Owner

Hi @Strokkur424

Thanks for the PR. I agree this solves a real problem for MDX users: import statements at the top of a document make excerpts noisy, and stripping them is useful.

That said, because remove-markdown is widely used, I want to be conservative about changing default output behavior. This should be opt-in rather than enabled by default.

Could you update the PR with the following changes?

  1. Add an option, probably named stripMdxImports, defaulting to false.

    Existing behavior should remain unchanged unless the caller explicitly enables it:

   removeMd(markdown, { stripMdxImports: true })

Please also update README.md and index.d.ts for the new option.
Avoid stripping imports from fenced code blocks.
The current implementation runs after fenced code blocks are converted to plain text, which creates a regression:

import x from "pkg";
console.log(x);

Today, remove-markdown preserves code block contents. With this PR, the import x from "pkg"; line is removed. We should not change that behavior, even when MDX import stripping is enabled.

Please make the matching a little more robust for common cases:
imports at end of file with no trailing newline
CRLF line endings
trailing whitespace after the semicolon
side-effect imports like import "pkg";
default/named/namespace/type imports
I’m okay if we do not try to parse every valid JavaScript import form. A conservative MDX-focused implementation is fine. But the behavior should be documented and covered by tests.

Please add regression tests for:
default behavior does not strip imports
{ stripMdxImports: true } strips top-level MDX imports
imports inside fenced code blocks are preserved
EOF import with no final newline
CRLF input
multiple consecutive import lines

I think the feature is worth adding, but I do not want to merge it as an unconditional behavior change. If we put it behind an option and protect existing code-block behavior, this should be much safer for downstream users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants