forked from magda-io/magda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move markdownToHtml to magda-typescript-common
- Loading branch information
Showing
11 changed files
with
53 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import MarkdownIt from "markdown-it"; | ||
const DOMPurify = require("dompurify/dist/purify"); | ||
|
||
const md = new MarkdownIt({ | ||
html: true, | ||
linkify: true, | ||
breaks: true | ||
}); | ||
|
||
const htmlRegex = /^\s*<[^>]+>/; | ||
|
||
export default function markdownToHtml( | ||
markdownString: string, | ||
allowUnsafeHtml: boolean = false, | ||
options = { | ||
FORBID_TAGS: ["svg", "math"] | ||
} | ||
) { | ||
if (!markdownString || typeof markdownString !== "string") { | ||
return markdownString; | ||
} | ||
// If the text looks like html, don't try to interpret it as Markdown because | ||
// we'll probably break it in the process. | ||
var unsafeHtml; | ||
if (htmlRegex.test(markdownString)) { | ||
unsafeHtml = markdownString; | ||
} else { | ||
// Note this would wrap non-standard tags such as <collapsible>hi</collapsible> in a <p></p>, which is bad. | ||
unsafeHtml = md.render(markdownString); | ||
} | ||
if (allowUnsafeHtml) { | ||
return unsafeHtml; | ||
} else { | ||
return DOMPurify.sanitize(unsafeHtml, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters