FEATURE: consistent bbcode inline and block rulers
This commit is contained in:
parent
234694b50f
commit
05e5a740a6
|
@ -26,11 +26,11 @@ function createHelper(featureName, opts, optionCallbacks, pluginCallbacks, getOp
|
|||
// hack to allow moving of getOptions
|
||||
helper.getOptions = () => getOptions.f();
|
||||
|
||||
helper.registerOptions = function(callback){
|
||||
helper.registerOptions = (callback) => {
|
||||
optionCallbacks.push([featureName, callback]);
|
||||
};
|
||||
|
||||
helper.registerPlugin = function(callback){
|
||||
helper.registerPlugin = (callback) => {
|
||||
pluginCallbacks.push([featureName, callback]);
|
||||
};
|
||||
|
||||
|
@ -57,6 +57,10 @@ function setupBlockBBCode(md) {
|
|||
md.block.bbcode_ruler = new Ruler();
|
||||
}
|
||||
|
||||
function setupInlineBBCode(md) {
|
||||
md.inline.bbcode_ruler = new Ruler();
|
||||
}
|
||||
|
||||
export function setup(opts, siteSettings, state) {
|
||||
if (opts.setup) {
|
||||
return;
|
||||
|
@ -117,6 +121,7 @@ export function setup(opts, siteSettings, state) {
|
|||
});
|
||||
|
||||
setupBlockBBCode(opts.engine);
|
||||
setupInlineBBCode(opts.engine);
|
||||
|
||||
pluginCallbacks.forEach(([feature, callback])=>{
|
||||
if (opts.discourse.features[feature]) {
|
||||
|
|
|
@ -46,7 +46,7 @@ export function parseBBCodeTag(src, start, max) {
|
|||
}
|
||||
|
||||
if (closed) {
|
||||
length = i;
|
||||
length = (i-start)+1;
|
||||
|
||||
let raw = src.slice(start+tag.length+1, i);
|
||||
|
||||
|
@ -168,14 +168,35 @@ function applyBBCode(state, startLine, endLine, silent, md) {
|
|||
// this will prevent lazy continuations from ever going past our end marker
|
||||
state.lineMax = nextLine;
|
||||
|
||||
rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1));
|
||||
if (rule.before) {
|
||||
rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1));
|
||||
}
|
||||
|
||||
let wrapTag;
|
||||
if (rule.wrap) {
|
||||
let split = rule.wrap.split('.');
|
||||
wrapTag = split[0];
|
||||
let className = split.slice(1).join(' ');
|
||||
|
||||
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);
|
||||
|
||||
rule.after.call(this, state, lastToken, md);
|
||||
if (rule.wrap) {
|
||||
state.push('wrap_bbcode', wrapTag, -1);
|
||||
}
|
||||
|
||||
if (rule.after) {
|
||||
rule.after.call(this, state, lastToken, md);
|
||||
}
|
||||
|
||||
lastToken = state.tokens[state.tokens.length-1];
|
||||
|
||||
|
@ -187,8 +208,6 @@ function applyBBCode(state, startLine, endLine, silent, md) {
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function setup(helper) {
|
||||
if (!helper.markdownIt) { return; }
|
||||
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
import { parseBBCodeTag } from 'pretty-text/engines/markdown-it/bbcode-block';
|
||||
|
||||
const rules = {
|
||||
'b': {tag: 'span', 'class': 'bbcode-b'},
|
||||
'i': {tag: 'span', 'class': 'bbcode-i'},
|
||||
'u': {tag: 'span', 'class': 'bbcode-u'},
|
||||
's': {tag: 'span', 'class': 'bbcode-s'}
|
||||
};
|
||||
|
||||
function tokanizeBBCode(state, silent) {
|
||||
function tokanizeBBCode(state, silent, ruler) {
|
||||
|
||||
let pos = state.pos;
|
||||
|
||||
|
@ -22,7 +15,17 @@ function tokanizeBBCode(state, silent) {
|
|||
return false;
|
||||
}
|
||||
|
||||
const rule = rules[tagInfo.tag];
|
||||
let rules = ruler.getRules();
|
||||
let rule;
|
||||
|
||||
for (let i=0; i<rules.length; i++) {
|
||||
let r = rules[i].rule;
|
||||
if (r.tag === tagInfo.tag) {
|
||||
rule = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rule) {
|
||||
return false;
|
||||
}
|
||||
|
@ -74,17 +77,24 @@ function processBBCode(state, silent) {
|
|||
|
||||
endDelim = delimiters[startDelim.end];
|
||||
|
||||
let split = tagInfo.rule.wrap.split('.');
|
||||
let tag = split[0];
|
||||
let className = split.slice(1).join(' ');
|
||||
|
||||
token = state.tokens[startDelim.token];
|
||||
|
||||
token.type = 'bbcode_' + tagInfo.tag + '_open';
|
||||
token.attrs = [['class', tagInfo.rule['class']]];
|
||||
token.tag = tagInfo.rule.tag;
|
||||
token.tag = tag;
|
||||
if (className) {
|
||||
token.attrs = [['class', className]];
|
||||
}
|
||||
token.nesting = 1;
|
||||
token.markup = token.content;
|
||||
token.content = '';
|
||||
|
||||
token = state.tokens[endDelim.token];
|
||||
token.type = 'bbcode_' + tagInfo.tag + '_close';
|
||||
token.tag = tagInfo.rule.tag;
|
||||
token.tag = tag;
|
||||
token.nesting = -1;
|
||||
token.markup = token.content;
|
||||
token.content = '';
|
||||
|
@ -103,7 +113,29 @@ export function setup(helper) {
|
|||
});
|
||||
|
||||
helper.registerPlugin(md => {
|
||||
md.inline.ruler.push('bbcode-inline', tokanizeBBCode);
|
||||
const ruler = md.inline.bbcode_ruler;
|
||||
|
||||
md.inline.ruler.push('bbcode-inline', (state,silent) => tokanizeBBCode(state,silent,ruler));
|
||||
md.inline.ruler2.before('text_collapse', 'bbcode-inline', processBBCode);
|
||||
|
||||
ruler.push('bold', {
|
||||
tag: 'b',
|
||||
wrap: 'span.bbcode-b',
|
||||
});
|
||||
|
||||
ruler.push('italic', {
|
||||
tag: 'i',
|
||||
wrap: 'span.bbcode-i'
|
||||
});
|
||||
|
||||
ruler.push('underline', {
|
||||
tag: 'u',
|
||||
wrap: 'span.bbcode-u'
|
||||
});
|
||||
|
||||
ruler.push('strike', {
|
||||
tag: 's',
|
||||
wrap: 'span.bbcode-s'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue