Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use shared function for rendering markdown
Fixes #1740
- Loading branch information
1 parent
a0e7d0e
commit b47b35c
Showing
19 changed files
with
94 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
"deprecation": { | ||
"severity": "warning" | ||
}, | ||
"import-blacklist": [true, "highlight.js"], | ||
"jsx-ban-props": false | ||
} | ||
} |
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
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,60 @@ | ||
import { highlight, highlightAuto } from 'highlight.js/lib/highlight' | ||
// tslint:disable-next-line:import-blacklist this is the only file allowed to import this module, all other modules must use renderMarkdown() exported from here | ||
import marked from 'marked' | ||
import sanitize from 'sanitize-html' | ||
|
||
/** | ||
* Escapes HTML by replacing characters like `<` with their HTML escape sequences like `<` | ||
*/ | ||
const escapeHTML = (html: string): string => { | ||
const span = document.createElement('span') | ||
span.textContent = html | ||
return span.innerHTML | ||
} | ||
|
||
/** | ||
* Attempts to syntax-highlight the given code. | ||
* If the language is not given, it is auto-detected. | ||
* If an error occurs, the code is returned as plain text with escaped HTML entities | ||
* | ||
* @param code The code to highlight | ||
* @param language The language of the code, if known | ||
* @return Safe HTML | ||
*/ | ||
export const highlightCodeSafe = (code: string, language?: string): string => { | ||
try { | ||
if (language === 'plaintext' || language === 'text') { | ||
return escapeHTML(code) | ||
} | ||
if (language) { | ||
return highlight(language, code, true).value | ||
} | ||
return highlightAuto(code).value | ||
} catch (err) { | ||
console.warn('Error syntax-highlighting hover markdown code block', err) | ||
return escapeHTML(code) | ||
} | ||
} | ||
|
||
/** | ||
* Renders the given markdown to HTML, highlighting code and sanitizing dangerous HTML. | ||
* Can throw an exception on parse errors. | ||
*/ | ||
export const renderMarkdown = (markdown: string): string => { | ||
const rendered = marked(markdown, { | ||
gfm: true, | ||
breaks: true, | ||
sanitize: false, | ||
highlight: (code, language) => highlightCodeSafe(code, language), | ||
}) | ||
return sanitize(rendered, { | ||
// Allow highligh.js styles, e.g. | ||
// <span class="hljs-keyword"> | ||
// <code class="language-javascript"> | ||
allowedTags: [...sanitize.defaults.allowedTags, 'span'], | ||
allowedAttributes: { | ||
span: ['class'], | ||
code: ['class'], | ||
}, | ||
}) | ||
} |
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
Oops, something went wrong.