support for bbcode [code] blocks

This commit is contained in:
Sam 2017-06-29 16:04:10 -04:00
parent de50d8cd35
commit d941ed90d6
3 changed files with 67 additions and 26 deletions

View File

@ -200,38 +200,44 @@ function applyBBCode(state, startLine, endLine, silent, md) {
// this will prevent lazy continuations from ever going past our end marker
state.lineMax = nextLine;
if (rule.before) {
rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1));
}
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 {
let wrapTag;
if (rule.wrap) {
let split = rule.wrap.split('.');
wrapTag = split[0];
let className = split.slice(1).join(' ');
if (rule.before) {
rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1));
}
let token = state.push('wrap_bbcode', wrapTag, 1);
let wrapTag;
if (rule.wrap) {
let split = rule.wrap.split('.');
wrapTag = split[0];
let className = split.slice(1).join(' ');
if (className) {
token.attrs = [['class', className]];
let token = state.push('wrap_bbcode', wrapTag, 1);
if (className) {
token.attrs = [['class', className]];
}
}
let lastToken = state.tokens[state.tokens.length-1];
lastToken.map = [ startLine, nextLine ];
state.md.block.tokenize(state, startLine + 1, nextLine);
if (rule.wrap) {
state.push('wrap_bbcode', wrapTag, -1);
}
if (rule.after) {
rule.after.call(this, state, lastToken, md, state.src.slice(start-2, start + closeTag.length - 1));
}
}
let lastToken = state.tokens[state.tokens.length-1];
lastToken.map = [ startLine, nextLine ];
state.md.block.tokenize(state, startLine + 1, nextLine);
if (rule.wrap) {
state.push('wrap_bbcode', wrapTag, -1);
}
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;
state.line = nextLine+1;
@ -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);

View File

@ -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) {

View File

@ -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