@wordpress/blocks
Version:
Block API for WordPress.
51 lines (47 loc) • 1.5 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = markdownConverter;
var _showdown = _interopRequireDefault(require("showdown"));
/**
* External dependencies
*/
// Reuse the same showdown converter.
const converter = new _showdown.default.Converter({
noHeaderId: true,
tables: true,
literalMidWordUnderscores: true,
omitExtraWLInCodeBlocks: true,
simpleLineBreaks: true,
strikethrough: true
});
/**
* Corrects the Slack Markdown variant of the code block.
* If uncorrected, it will be converted to inline code.
*
* @see https://get.slack.help/hc/en-us/articles/202288908-how-can-i-add-formatting-to-my-messages-#code-blocks
*
* @param {string} text The potential Markdown text to correct.
*
* @return {string} The corrected Markdown.
*/
function slackMarkdownVariantCorrector(text) {
return text.replace(/((?:^|\n)```)([^\n`]+)(```(?:$|\n))/, (match, p1, p2, p3) => `${p1}\n${p2}\n${p3}`);
}
function bulletsToAsterisks(text) {
return text.replace(/(^|\n)•( +)/g, '$1*$2');
}
/**
* Converts a piece of text into HTML based on any Markdown present.
* Also decodes any encoded HTML.
*
* @param {string} text The plain text to convert.
*
* @return {string} HTML.
*/
function markdownConverter(text) {
return converter.makeHtml(slackMarkdownVariantCorrector(bulletsToAsterisks(text)));
}
//# sourceMappingURL=markdown-converter.js.map