mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
67 lines
1.6 KiB
Plaintext
67 lines
1.6 KiB
Plaintext
|
import { parseBBCodeTag } from "pretty-text/engines/discourse-markdown/bbcode-block";
|
||
|
|
||
|
const WRAP_CLASS = "d-wrap";
|
||
|
|
||
|
function parseAttributes(tagInfo) {
|
||
|
const attributes = tagInfo.attrs._default || "";
|
||
|
|
||
|
return (
|
||
|
parseBBCodeTag(`[wrap wrap=${attributes}]`, 0, attributes.length + 12)
|
||
|
.attrs || {}
|
||
|
);
|
||
|
}
|
||
|
|
||
|
function applyDataAttributes(token, state, attributes) {
|
||
|
Object.keys(attributes).forEach(tag => {
|
||
|
const value = state.md.utils.escapeHtml(attributes[tag]);
|
||
|
tag = state.md.utils.escapeHtml(tag.replace(/[^a-z0-9\-]/g, ""));
|
||
|
|
||
|
if (value && tag && tag.length > 1) {
|
||
|
token.attrs.push([`data-${tag}`, value]);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const blockRule = {
|
||
|
tag: "wrap",
|
||
|
|
||
|
before(state, tagInfo) {
|
||
|
let token = state.push("wrap_open", "div", 1);
|
||
|
token.attrs = [["class", WRAP_CLASS]];
|
||
|
|
||
|
applyDataAttributes(token, state, parseAttributes(tagInfo));
|
||
|
},
|
||
|
|
||
|
after(state) {
|
||
|
state.push("wrap_close", "div", -1);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const inlineRule = {
|
||
|
tag: "wrap",
|
||
|
|
||
|
replace(state, tagInfo, content) {
|
||
|
let token = state.push("wrap_open", "span", 1);
|
||
|
token.attrs = [["class", WRAP_CLASS]];
|
||
|
|
||
|
applyDataAttributes(token, state, parseAttributes(tagInfo));
|
||
|
|
||
|
if (content) {
|
||
|
token = state.push("text", "", 0);
|
||
|
token.content = content;
|
||
|
}
|
||
|
|
||
|
token = state.push("wrap_close", "span", -1);
|
||
|
return true;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
export function setup(helper) {
|
||
|
helper.registerPlugin(md => {
|
||
|
md.inline.bbcode.ruler.push("inline-wrap", inlineRule);
|
||
|
md.block.bbcode.ruler.push("block-wrap", blockRule);
|
||
|
});
|
||
|
|
||
|
helper.whiteList([`div.${WRAP_CLASS}`, `span.${WRAP_CLASS}`, "span[data-*]"]);
|
||
|
}
|