Skip to content

Commit

Permalink
support for bbcode [code] blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Jun 29, 2017
1 parent de50d8c commit d941ed9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
Expand Up @@ -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 ];
let lastToken = state.tokens[state.tokens.length-1];
lastToken.map = [ startLine, nextLine ];

state.md.block.tokenize(state, startLine + 1, nextLine);
state.md.block.tokenize(state, startLine + 1, nextLine);

if (rule.wrap) {
state.push('wrap_bbcode', wrapTag, -1);
}
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));
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;
Expand All @@ -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);
Expand Down
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions spec/components/pretty_text_spec.rb
Expand Up @@ -784,6 +784,18 @@ def strip_image_wrapping(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

0 comments on commit d941ed9

Please sign in to comment.