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 .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"printWidth": 80,
"endOfLine": "auto"
}
29 changes: 23 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Options = {
gfm?: boolean;
useImgAltText?: boolean;
preserveLinks?: boolean;
preserveKaTeX?: boolean;
};

/**
Expand All @@ -21,7 +22,7 @@ const removeMarkdown = (
markdown: string,
options: Options = {
listUnicodeChar: "",
}
},
) => {
options = options || {};
options.listUnicodeChar = options.hasOwnProperty("listUnicodeChar")
Expand All @@ -34,10 +35,17 @@ const removeMarkdown = (
options.useImgAltText = options.hasOwnProperty("useImgAltText")
? options.useImgAltText
: true;
options.preserveLinks = options.hasOwnProperty("preserveLinks") ? options.preserveLinks : false;
options.preserveLinks = options.hasOwnProperty("preserveLinks")
? options.preserveLinks
: false;
options.preserveKaTeX = options.hasOwnProperty("preserveKaTeX")
? options.preserveKaTeX
: false;

let output = markdown || "";

// Remove the frontmatter in markdown.
output = output.replace(/^---[\s\S]*?^---\s*/m, "");
// Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top)
output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, "");

Expand All @@ -46,7 +54,7 @@ const removeMarkdown = (
if (options.listUnicodeChar)
output = output.replace(
/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm,
options.listUnicodeChar + " $1"
options.listUnicodeChar + " $1",
);
else output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, "$1");
}
Expand All @@ -61,9 +69,18 @@ const removeMarkdown = (
// Fenced codeblocks
.replace(/`{3}.*\n/g, "");
}
if(options.preserveLinks) {
if (!options.preserveKaTeX) {
output = output
// Remove KaTeX wrapped by $$...$$
.replace(/\$\$([\s\S]*?)\$\$/g, "")
// Remove inline KaTeX
.replace(/\$.*?\$/g, "")
// Remove KaTeX wrapped by $$\[...\]$$
.replace(/\\\[(.|\n)*?\\\]/g, "");
}
if (options.preserveLinks) {
// Remove inline links while preserving the links
output = output.replace(/\[(.*?)\][\[\(](.*?)[\]\)]/g, "$1 ($2)")
output = output.replace(/\[(.*?)\][\[\(](.*?)[\]\)]/g, "$1 ($2)");
}
output = output
// Remove HTML tags
Expand All @@ -85,7 +102,7 @@ const removeMarkdown = (
// Remove atx-style headers
.replace(
/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm,
"$1$2$3"
"$1$2$3",
)
// Remove emphasis (repeat the line to remove double emphasis)
.replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, "$2")
Expand Down
Loading