Added github style HTML sanitization to .md convertion

This commit is contained in:
Jori Lallo
2016-08-02 11:29:07 +03:00
parent af14bc3cb5
commit 4455877585
2 changed files with 16 additions and 16 deletions

View File

@@ -1,16 +1,18 @@
import slug from 'slug'; import slug from 'slug';
import marked, { Renderer } from 'marked'; import marked from 'marked';
import sanitizedRenderer from 'marked-sanitized';
import highlight from 'highlight.js'; import highlight from 'highlight.js';
import emojify from './emojify'; import emojify from './emojify';
import _escape from 'lodash/escape'; import _escape from 'lodash/escape';
slug.defaults.mode ='rfc3986'; slug.defaults.mode = 'rfc3986';
const Renderer = sanitizedRenderer(marked.Renderer);
const renderer = new Renderer(); const renderer = new Renderer();
renderer.code = (code, language) => { renderer.code = (code, language) => {
const validLang = !!(language && highlight.getLanguage(language)); const validLang = !!(language && highlight.getLanguage(language));
const highlighted = validLang ? highlight.highlight(language, code).value : _escape(code); const highlighted = validLang ? highlight.highlight(language, code).value : _escape(code);
return `<pre><code class="hljs ${language}">${ highlighted }</code></pre>`; return `<pre><code class="hljs ${language}">${highlighted}</code></pre>`;
}; };
renderer.heading = (text, level) => { renderer.heading = (text, level) => {
const headingSlug = slug(text); const headingSlug = slug(text);
@@ -20,22 +22,19 @@ renderer.heading = (text, level) => {
<a name="${headingSlug}" class="anchor" href="#${headingSlug}">#</a> <a name="${headingSlug}" class="anchor" href="#${headingSlug}">#</a>
</h${level}> </h${level}>
`; `;
}, };
marked.setOptions({
renderer: renderer,
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: true,
});
// TODO: This is syncronous and can be costly // TODO: This is syncronous and can be costly
const convertToMarkdown = (text) => { const convertToMarkdown = (text) => {
return marked(emojify(text)); return marked.parse(emojify(text), {
renderer,
gfm: true,
tables: true,
breaks: false,
pedantic: false,
smartLists: true,
smartypants: true,
});
}; };
export { export {

View File

@@ -79,6 +79,7 @@
"lodash": "4.13.1", "lodash": "4.13.1",
"lodash.orderby": "4.4.0", "lodash.orderby": "4.4.0",
"marked": "0.3.6", "marked": "0.3.6",
"marked-sanitized": "^0.1.1",
"mobx": "2.4.1", "mobx": "2.4.1",
"mobx-react": "3.4.0", "mobx-react": "3.4.0",
"mobx-react-devtools": "4.2.0", "mobx-react-devtools": "4.2.0",