Extracted markdown-html-renderer to @tryghost/kg-markdown-html-renderer

no issue

- prep for further extraction of mobiledoc cards and renderer
This commit is contained in:
Kevin Ansfield 2020-03-25 13:34:26 +00:00
parent 299c08b079
commit c65c9c1e5e
5 changed files with 20 additions and 68 deletions

View File

@ -9,11 +9,11 @@ module.exports = function markdownCardDefinition() {
}, },
render: function (opts) { render: function (opts) {
let renderers = require('../renderers'); let markdownHtmlRenderer = require('@tryghost/kg-markdown-html-renderer');
let payload = opts.payload; let payload = opts.payload;
let version = opts.options && opts.options.version || 2; let version = opts.options && opts.options.version || 2;
// convert markdown to HTML ready for insertion into dom // convert markdown to HTML ready for insertion into dom
let html = renderers.markdownHtmlRenderer.render(payload.markdown || ''); let html = markdownHtmlRenderer.render(payload.markdown || '');
if (!html) { if (!html) {
return ''; return '';

View File

@ -1,9 +1,5 @@
module.exports = { module.exports = {
get mobiledocHtmlRenderer() { get mobiledocHtmlRenderer() {
return require('./mobiledoc-html-renderer'); return require('./mobiledoc-html-renderer');
},
get markdownHtmlRenderer() {
return require('./markdown-html-renderer');
} }
}; };

View File

@ -1,54 +0,0 @@
const MarkdownIt = require('markdown-it');
const markdownIt = new MarkdownIt({
html: true,
breaks: true,
linkify: true
})
.use(require('markdown-it-footnote'))
.use(require('markdown-it-lazy-headers'))
.use(require('markdown-it-mark'))
.use(function namedHeaders(md) {
// match legacy Showdown IDs
const slugify = function (inputString, usedHeaders) {
let slug = inputString.replace(/[^\w]/g, '').toLowerCase();
if (usedHeaders[slug]) {
usedHeaders[slug] += 1;
slug += usedHeaders[slug];
}
return slug;
};
const originalHeadingOpen = md.renderer.rules.heading_open;
// originally from https://github.com/leff/markdown-it-named-headers
// moved here to avoid pulling in http://stringjs.com dependency
md.renderer.rules.heading_open = function (tokens, idx, something, somethingelse, self) {
const usedHeaders = {};
tokens[idx].attrs = tokens[idx].attrs || [];
const title = tokens[idx + 1].children.reduce(function (acc, t) {
return acc + t.content;
}, '');
const slug = slugify(title, usedHeaders);
tokens[idx].attrs.push(['id', slug]);
if (originalHeadingOpen) {
return originalHeadingOpen.apply(this, arguments);
} else {
return self.renderToken.apply(self, arguments);
}
};
});
// configure linkify-it
markdownIt.linkify.set({
fuzzyLink: false
});
module.exports = {
render: function (markdown) {
return markdownIt.render(markdown);
}
};

View File

@ -43,6 +43,7 @@
"@sentry/node": "5.15.0", "@sentry/node": "5.15.0",
"@tryghost/errors": "^0.1.0", "@tryghost/errors": "^0.1.0",
"@tryghost/helpers": "1.1.23", "@tryghost/helpers": "1.1.23",
"@tryghost/kg-markdown-html-renderer": "1.0.0",
"@tryghost/members-api": "0.18.0", "@tryghost/members-api": "0.18.0",
"@tryghost/members-ssr": "0.7.4", "@tryghost/members-ssr": "0.7.4",
"@tryghost/social-urls": "0.1.6", "@tryghost/social-urls": "0.1.6",
@ -94,10 +95,6 @@
"knex-migrator": "3.4.2", "knex-migrator": "3.4.2",
"lodash": "4.17.15", "lodash": "4.17.15",
"mailgun-js": "0.22.0", "mailgun-js": "0.22.0",
"markdown-it": "10.0.0",
"markdown-it-footnote": "3.0.2",
"markdown-it-lazy-headers": "0.1.3",
"markdown-it-mark": "3.0.0",
"metascraper": "5.11.6", "metascraper": "5.11.6",
"metascraper-author": "5.11.6", "metascraper-author": "5.11.6",
"metascraper-description": "5.11.6", "metascraper-description": "5.11.6",

View File

@ -329,6 +329,19 @@
resolved "https://registry.yarnpkg.com/@tryghost/kg-clean-basic-html/-/kg-clean-basic-html-0.1.5.tgz#21cbe9036472c04b2320b1feb359c9c40ff3a300" resolved "https://registry.yarnpkg.com/@tryghost/kg-clean-basic-html/-/kg-clean-basic-html-0.1.5.tgz#21cbe9036472c04b2320b1feb359c9c40ff3a300"
integrity sha512-jl+NKYryIL+KUQb4XNcMgu9l/K12oASgZYuRqi6GCm0JKhz1qD56vavIpKAlPjJyMOhSfD0MicoX887ijgvqyQ== integrity sha512-jl+NKYryIL+KUQb4XNcMgu9l/K12oASgZYuRqi6GCm0JKhz1qD56vavIpKAlPjJyMOhSfD0MicoX887ijgvqyQ==
"@tryghost/kg-markdown-html-renderer@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@tryghost/kg-markdown-html-renderer/-/kg-markdown-html-renderer-1.0.0.tgz#7a68a5700955b031b29b3a70f7ab5b5391d7746e"
integrity sha512-MC+rxwaxkM7g+EkihYr6dmyc8CUkVVahDxjxEcKlVAVWpnB8GN/paaS1Lgv9n9KxqEHCuvewhCDPwH93sczTNg==
dependencies:
bluebird "^3.7.2"
ghost-ignition "^4.1.0"
lodash "^4.17.15"
markdown-it "^10.0.0"
markdown-it-footnote "^3.0.2"
markdown-it-lazy-headers "^0.1.3"
markdown-it-mark "^3.0.0"
"@tryghost/kg-parser-plugins@0.9.0": "@tryghost/kg-parser-plugins@0.9.0":
version "0.9.0" version "0.9.0"
resolved "https://registry.yarnpkg.com/@tryghost/kg-parser-plugins/-/kg-parser-plugins-0.9.0.tgz#6ff9a0cb8101978787bfa3f77d2da652c2d786b1" resolved "https://registry.yarnpkg.com/@tryghost/kg-parser-plugins/-/kg-parser-plugins-0.9.0.tgz#6ff9a0cb8101978787bfa3f77d2da652c2d786b1"
@ -5439,22 +5452,22 @@ markdown-escapes@^1.0.0:
resolved "https://registry.yarnpkg.com/markdown-escapes/-/markdown-escapes-1.0.3.tgz#6155e10416efaafab665d466ce598216375195f5" resolved "https://registry.yarnpkg.com/markdown-escapes/-/markdown-escapes-1.0.3.tgz#6155e10416efaafab665d466ce598216375195f5"
integrity sha512-XUi5HJhhV5R74k8/0H2oCbCiYf/u4cO/rX8tnGkRvrqhsr5BRNU6Mg0yt/8UIx1iIS8220BNJsDb7XnILhLepw== integrity sha512-XUi5HJhhV5R74k8/0H2oCbCiYf/u4cO/rX8tnGkRvrqhsr5BRNU6Mg0yt/8UIx1iIS8220BNJsDb7XnILhLepw==
markdown-it-footnote@3.0.2: markdown-it-footnote@^3.0.2:
version "3.0.2" version "3.0.2"
resolved "https://registry.yarnpkg.com/markdown-it-footnote/-/markdown-it-footnote-3.0.2.tgz#1575ee7a093648d4e096aa33386b058d92ac8bc1" resolved "https://registry.yarnpkg.com/markdown-it-footnote/-/markdown-it-footnote-3.0.2.tgz#1575ee7a093648d4e096aa33386b058d92ac8bc1"
integrity sha512-JVW6fCmZWjvMdDQSbOT3nnOQtd9iAXmw7hTSh26+v42BnvXeVyGMDBm5b/EZocMed2MbCAHiTX632vY0FyGB8A== integrity sha512-JVW6fCmZWjvMdDQSbOT3nnOQtd9iAXmw7hTSh26+v42BnvXeVyGMDBm5b/EZocMed2MbCAHiTX632vY0FyGB8A==
markdown-it-lazy-headers@0.1.3: markdown-it-lazy-headers@^0.1.3:
version "0.1.3" version "0.1.3"
resolved "https://registry.yarnpkg.com/markdown-it-lazy-headers/-/markdown-it-lazy-headers-0.1.3.tgz#e70dd4da79c87a9ce82ca4701b8b7c0e2d72297b" resolved "https://registry.yarnpkg.com/markdown-it-lazy-headers/-/markdown-it-lazy-headers-0.1.3.tgz#e70dd4da79c87a9ce82ca4701b8b7c0e2d72297b"
integrity sha1-5w3U2nnIepzoLKRwG4t8Di1yKXs= integrity sha1-5w3U2nnIepzoLKRwG4t8Di1yKXs=
markdown-it-mark@3.0.0: markdown-it-mark@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/markdown-it-mark/-/markdown-it-mark-3.0.0.tgz#27c3e39ef3cc310b2dde5375082c9fa912983cda" resolved "https://registry.yarnpkg.com/markdown-it-mark/-/markdown-it-mark-3.0.0.tgz#27c3e39ef3cc310b2dde5375082c9fa912983cda"
integrity sha512-HqMWeKfMMOu4zBO0emmxsoMWmbf2cPKZY1wP6FsTbKmicFfp5y4L3KXAsNeO1rM6NTJVOrNlLKMPjWzriBGspw== integrity sha512-HqMWeKfMMOu4zBO0emmxsoMWmbf2cPKZY1wP6FsTbKmicFfp5y4L3KXAsNeO1rM6NTJVOrNlLKMPjWzriBGspw==
markdown-it@10.0.0: markdown-it@^10.0.0:
version "10.0.0" version "10.0.0"
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-10.0.0.tgz#abfc64f141b1722d663402044e43927f1f50a8dc" resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-10.0.0.tgz#abfc64f141b1722d663402044e43927f1f50a8dc"
integrity sha512-YWOP1j7UbDNz+TumYP1kpwnP0aEa711cJjrAQrzd0UXlbJfc5aAq0F/PZHjiioqDC1NKgvIMX+o+9Bk7yuM2dg== integrity sha512-YWOP1j7UbDNz+TumYP1kpwnP0aEa711cJjrAQrzd0UXlbJfc5aAq0F/PZHjiioqDC1NKgvIMX+o+9Bk7yuM2dg==