diff --git a/core/server/utils/markdown-converter.js b/core/server/utils/markdown-converter.js index 8c8d97135b..c32fe656f9 100644 --- a/core/server/utils/markdown-converter.js +++ b/core/server/utils/markdown-converter.js @@ -7,16 +7,41 @@ var MarkdownIt = require('markdown-it'), .use(require('markdown-it-footnote')) .use(require('markdown-it-lazy-headers')) .use(require('markdown-it-mark')) - .use(require('markdown-it-named-headers'), { - // match legacy Showdown IDs otherwise default is github style dasherized - slugify: function (inputString, usedHeaders) { + .use(function namedHeaders(md) { + // jscs:disable + + // match legacy Showdown IDs + var slugify = function (inputString, usedHeaders) { var slug = inputString.replace(/[^\w]/g, '').toLowerCase(); if (usedHeaders[slug]) { usedHeaders[slug] += 1; slug += usedHeaders[slug]; } return slug; - } + }; + var 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) { + var used_headers = {}; + + tokens[idx].attrs = tokens[idx].attrs || []; + + var title = tokens[idx + 1].children.reduce(function (acc, t) { + return acc + t.content; + }, ''); + + var slug = slugify(title, used_headers); + tokens[idx].attrs.push(['id', slug]); + + if (originalHeadingOpen) { + return originalHeadingOpen.apply(this, arguments); + } else { + return self.renderToken.apply(self, arguments); + } + }; + // jscs:enable }); module.exports = { diff --git a/package.json b/package.json index 2ff5c34336..e9f574cf7a 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,6 @@ "markdown-it-footnote": "3.0.1", "markdown-it-lazy-headers": "0.1.3", "markdown-it-mark": "2.0.0", - "markdown-it-named-headers": "0.0.4", "mobiledoc-dom-renderer": "0.6.5", "moment": "2.18.1", "moment-timezone": "0.5.13", diff --git a/yarn.lock b/yarn.lock index 36e80fbe5d..1e1bf555d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3311,12 +3311,6 @@ markdown-it-mark@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/markdown-it-mark/-/markdown-it-mark-2.0.0.tgz#46a1aa947105aed8188978e0a016179e404f42c7" -markdown-it-named-headers@0.0.4: - version "0.0.4" - resolved "https://registry.yarnpkg.com/markdown-it-named-headers/-/markdown-it-named-headers-0.0.4.tgz#82efc28324240a6b1e77b9aae501771d5f351c1f" - dependencies: - string "^3.0.1" - markdown-it@8.4.0: version "8.4.0" resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-8.4.0.tgz#e2400881bf171f7018ed1bd9da441dac8af6306d" @@ -5040,10 +5034,6 @@ string-width@^1.0.1: is-fullwidth-code-point "^1.0.0" strip-ansi "^3.0.0" -string@^3.0.1: - version "3.3.3" - resolved "https://registry.yarnpkg.com/string/-/string-3.3.3.tgz#5ea211cd92d228e184294990a6cc97b366a77cb0" - string_decoder@~0.10.x: version "0.10.31" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"