FEATURE: Customizable rules and plugins for PrettyText.markdown.

This commit extends the options which can be passed to
`PrettyText.markdown` so that which Markdown-it rules and Discourse
Markdown plugins to be used when rendering a text can be customizable.
Currently, this extension is mainly used by plugins.
This commit is contained in:
Alan Guo Xiang Tan
2022-01-06 15:27:12 +08:00
parent 9f5c8644d0
commit c2afc3915b
5 changed files with 102 additions and 8 deletions

View File

@@ -1493,6 +1493,45 @@ var bar = 'bar';
);
});
test("customizing markdown-it rules", function (assert) {
assert.cookedOptions(
"**bold**",
{ markdownItRules: [] },
"<p>**bold**</p>",
"does not apply bold markdown when rule is not enabled"
);
assert.cookedOptions(
"**bold**",
{ markdownItRules: ["emphasis"] },
"<p><strong>bold</strong></p>",
"applies bold markdown when rule is enabled"
);
});
test("features override", function (assert) {
assert.cookedOptions(
":grin: @sam",
{ featuresOverride: [] },
"<p>:grin: @sam</p>",
"does not cook emojis when Discourse markdown engines are disabled"
);
assert.cookedOptions(
":grin: @sam",
{ featuresOverride: ["emoji"] },
'<p><img src="/images/emoji/google_classic/grin.png?v=10" title=":grin:" class="emoji" alt=":grin:"> @sam</p>',
"cooks emojis when only the emoji markdown engine is enabled"
);
assert.cookedOptions(
":grin: @sam",
{ featuresOverride: ["mentions", "text-post-process"] },
`<p>:grin: <span class="mention">@sam</span></p>`,
"cooks mentions when only the mentions markdown engine is enabled"
);
});
test("emoji", function (assert) {
assert.cooked(
":smile:",

View File

@@ -353,6 +353,12 @@ export function setup(opts, siteSettings, state) {
}
});
if (opts.featuresOverride) {
Object.keys(opts.features).forEach((feature) => {
opts.features[feature] = opts.featuresOverride.includes(feature);
});
}
let copy = {};
Object.keys(opts).forEach((entry) => {
copy[entry] = opts[entry];
@@ -371,14 +377,22 @@ export function setup(opts, siteSettings, state) {
enableDiffhtmlPreview: siteSettings.enable_diffhtml_preview,
};
opts.engine = window.markdownit({
const markdownitOpts = {
discourse: opts.discourse,
html: true,
breaks: !siteSettings.traditional_markdown_linebreaks,
xhtmlOut: false,
linkify: siteSettings.enable_markdown_linkify,
typographer: siteSettings.enable_markdown_typographer,
});
};
if (opts.discourse.markdownItRules !== undefined) {
opts.engine = window
.markdownit("zero", markdownitOpts) // Preset for "zero", https://github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.js
.enable(opts.discourse.markdownItRules);
} else {
opts.engine = window.markdownit(markdownitOpts);
}
const quotation_marks = siteSettings.markdown_typographer_quotation_marks;
if (quotation_marks) {

View File

@@ -38,6 +38,8 @@ export function buildOptions(state) {
customEmojiTranslation,
watchedWordsReplace,
watchedWordsLink,
featuresOverride,
markdownItRules,
} = state;
let features = {};
@@ -76,6 +78,8 @@ export function buildOptions(state) {
disableEmojis,
watchedWordsReplace,
watchedWordsLink,
featuresOverride,
markdownItRules,
};
// note, this will mutate options due to the way the API is designed