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.

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.

1 participant