mirror of
https://github.com/discourse/discourse.git
synced 2024-12-05 12:43:43 +08:00
a582c49601
Backport ffc49ab46b
65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
// see: https://github.com/markdown-it/markdown-it/issues/375
|
|
//
|
|
// we use a custom paragraph rule cause we have to signal when a
|
|
// link starts with a space, so we can bypass a onebox
|
|
// this is a freedom patch, so careful, may break on updates
|
|
|
|
function newline(state, silent) {
|
|
let token,
|
|
pmax,
|
|
max,
|
|
ws,
|
|
pos = state.pos;
|
|
|
|
if (state.src.charCodeAt(pos) !== 0x0a /* \n */) {
|
|
return false;
|
|
}
|
|
|
|
pmax = state.pending.length - 1;
|
|
max = state.posMax;
|
|
|
|
// ' \n' -> hardbreak
|
|
// Lookup in pending chars is bad practice! Don't copy to other rules!
|
|
// Pending string is stored in concat mode, indexed lookups will cause
|
|
// conversion to flat mode.
|
|
if (!silent) {
|
|
if (pmax >= 0 && state.pending.charCodeAt(pmax) === 0x20) {
|
|
if (pmax >= 1 && state.pending.charCodeAt(pmax - 1) === 0x20) {
|
|
// Find whitespaces tail of pending chars.
|
|
ws = pmax - 1;
|
|
|
|
while (ws >= 1 && state.pending.charCodeAt(ws - 1) === 0x20) {
|
|
ws--;
|
|
}
|
|
|
|
state.pending = state.pending.slice(0, ws);
|
|
token = state.push("hardbreak", "br", 0);
|
|
} else {
|
|
state.pending = state.pending.slice(0, -1);
|
|
token = state.push("softbreak", "br", 0);
|
|
}
|
|
} else {
|
|
token = state.push("softbreak", "br", 0);
|
|
}
|
|
}
|
|
|
|
pos++;
|
|
|
|
// skip heading spaces for next line
|
|
while (pos < max && state.md.utils.isSpace(state.src.charCodeAt(pos))) {
|
|
if (token) {
|
|
token.leading_space = true;
|
|
}
|
|
pos++;
|
|
}
|
|
|
|
state.pos = pos;
|
|
return true;
|
|
}
|
|
|
|
export function setup(helper) {
|
|
helper.registerPlugin((md) => {
|
|
md.inline.ruler.at("newline", newline);
|
|
});
|
|
}
|