feat: strip MDX imports#113
Conversation
|
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 Could you update the PR with the following changes?
removeMd(markdown, { stripMdxImports: true })Please also update README.md and index.d.ts for the new option. import x from "pkg";
console.log(x);Today, Please make the matching a little more robust for common cases: Please add regression tests for: 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. |
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.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
importkeyword and any combination of the valid characters.I've added the following test case:
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.