mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-25 09:03:12 +03:00
f8f09dab05
refs #718, refs https://github.com/TryGhost/Ghost/pull/8305 - meta description preview in the PSM was relying on the `html` field which is no longer queried - see #718 and https://github.com/TryGhost/Ghost/pull/8305 - restores live preview that was in LTS but removed whilst implementing mobiledoc because we had no quick way of rendering mobiledoc->text - adds a boolean argument to the `formatMarkdown` util that can disable the replacement of `<script>` and `<iframe>` tags so that the inserted text isn't rendered when converting HTML to text
50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
/* global html_sanitize */
|
|
import cajaSanitizers from './caja-sanitizers';
|
|
import markdownit from 'npm:markdown-it';
|
|
import markdownitFootnote from 'npm:markdown-it-footnote';
|
|
import markdownitLazyHeaders from 'npm:markdown-it-lazy-headers';
|
|
import markdownitMark from 'npm:markdown-it-mark';
|
|
import markdownitNamedHeaders from 'npm:markdown-it-named-headers';
|
|
|
|
// eslint-disable-next-line new-cap
|
|
let md = markdownit({
|
|
html: true,
|
|
breaks: true,
|
|
linkify: true
|
|
})
|
|
.use(markdownitFootnote)
|
|
.use(markdownitLazyHeaders)
|
|
.use(markdownitMark)
|
|
.use(markdownitNamedHeaders, {
|
|
// match legacy Showdown IDs otherwise default is github style dasherized
|
|
slugify(inputString, usedHeaders) {
|
|
let slug = inputString.replace(/[^\w]/g, '').toLowerCase();
|
|
if (usedHeaders[slug]) {
|
|
usedHeaders[slug]++;
|
|
slug += usedHeaders[slug];
|
|
}
|
|
return slug;
|
|
}
|
|
});
|
|
|
|
export default function formatMarkdown(_markdown, replaceJS = true) {
|
|
let markdown = _markdown || '';
|
|
let escapedhtml = '';
|
|
|
|
// convert markdown to HTML
|
|
escapedhtml = md.render(markdown);
|
|
|
|
// replace script and iFrame
|
|
if (replaceJS) {
|
|
escapedhtml = escapedhtml.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
|
|
'<pre class="js-embed-placeholder">Embedded JavaScript</pre>');
|
|
escapedhtml = escapedhtml.replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi,
|
|
'<pre class="iframe-embed-placeholder">Embedded iFrame</pre>');
|
|
}
|
|
|
|
// sanitize html
|
|
escapedhtml = html_sanitize(escapedhtml, cajaSanitizers.url, cajaSanitizers.id);
|
|
|
|
return escapedhtml;
|
|
}
|