mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 18:30:26 -06:00
support for bbcode [code] blocks
This commit is contained in:
parent
de50d8cd35
commit
d941ed90d6
@ -200,6 +200,13 @@ function applyBBCode(state, startLine, endLine, silent, md) {
|
||||
// this will prevent lazy continuations from ever going past our end marker
|
||||
state.lineMax = nextLine;
|
||||
|
||||
if (rule.replace) {
|
||||
let content = state.src.slice(state.bMarks[startLine+1], state.eMarks[nextLine-1]);
|
||||
if (!rule.replace.call(this, state, info, content)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
||||
if (rule.before) {
|
||||
rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1));
|
||||
}
|
||||
@ -229,8 +236,7 @@ function applyBBCode(state, startLine, endLine, silent, md) {
|
||||
if (rule.after) {
|
||||
rule.after.call(this, state, lastToken, md, state.src.slice(start-2, start + closeTag.length - 1));
|
||||
}
|
||||
|
||||
lastToken = state.tokens[state.tokens.length-1];
|
||||
}
|
||||
|
||||
state.parentType = old_parent;
|
||||
state.lineMax = old_line_max;
|
||||
@ -242,7 +248,20 @@ function applyBBCode(state, startLine, endLine, silent, md) {
|
||||
export function setup(helper) {
|
||||
if (!helper.markdownIt) { return; }
|
||||
|
||||
|
||||
helper.registerPlugin(md => {
|
||||
const ruler = md.block.bbcode_ruler;
|
||||
|
||||
ruler.push('code', {
|
||||
tag: 'code',
|
||||
replace: function(state, tagInfo, content) {
|
||||
let token;
|
||||
token = state.push('fence', 'code', 0);
|
||||
token.content = content;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
isWhiteSpace = md.utils.isWhiteSpace;
|
||||
md.block.ruler.after('fence', 'bbcode', (state, startLine, endLine, silent)=> {
|
||||
return applyBBCode(state, startLine, endLine, silent, md);
|
||||
|
@ -150,6 +150,16 @@ export function setup(helper) {
|
||||
md.inline.ruler.push('bbcode-inline', (state,silent) => tokanizeBBCode(state,silent,ruler));
|
||||
md.inline.ruler2.before('text_collapse', 'bbcode-inline', processBBCode);
|
||||
|
||||
ruler.push('code', {
|
||||
tag: 'code',
|
||||
replace: function(state, tagInfo, content) {
|
||||
let token;
|
||||
token = state.push('code_inline', 'code', 0);
|
||||
token.content = content;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
ruler.push('url', {
|
||||
tag: 'url',
|
||||
replace: function(state, tagInfo, content) {
|
||||
|
@ -784,6 +784,18 @@ HTML
|
||||
expect(cooked).to eq(html)
|
||||
end
|
||||
|
||||
it "supports inline code bbcode" do
|
||||
cooked = PrettyText.cook "Testing [code]codified **stuff** and `more` stuff[/code]"
|
||||
html = "<p>Testing <code>codified **stuff** and `more` stuff</code></p>"
|
||||
expect(cooked).to eq(html)
|
||||
end
|
||||
|
||||
it "supports block code bbcode" do
|
||||
cooked = PrettyText.cook "[code]\ncodified\n\n\n **stuff** and `more` stuff\n[/code]"
|
||||
html = "<pre><code class=\"lang-auto\">codified\n\n\n **stuff** and `more` stuff</code></pre>"
|
||||
expect(cooked).to eq(html)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user