-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
Do not remove escaping from brackets
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { Mark } = require('@tiptap/core') | ||
|
||
/** | ||
* Keep markdown untouched | ||
*/ | ||
const KeepSyntax = Mark.create({ | ||
name: 'keep-syntax', | ||
parseHTML() { | ||
return [ | ||
{ | ||
tag: 'span.keep-md', | ||
}, | ||
] | ||
}, | ||
renderHTML() { | ||
return ['span', { class: 'keep-md' }, 0] | ||
}, | ||
toMarkdown: { | ||
open: '', | ||
close: '', | ||
mixable: true, | ||
escape: false, | ||
expelEnclosingWhitespace: true, | ||
}, | ||
|
||
/** | ||
* Remove mark if there were manual changes | ||
*/ | ||
onUpdate() { | ||
const tr = this.editor.state.tr | ||
|
||
this.editor.state.doc.descendants((node, pos, parent, index) => { | ||
if (node.marks.findIndex(mark => mark.type.name === this.name) !== -1) { | ||
if (node.type.name !== 'text' || node.text.length !== 1) { | ||
tr.removeMark(pos, pos + node.nodeSize, this.type) | ||
} | ||
} | ||
}) | ||
if (tr.docChanged) { | ||
tr.setMeta('addToHistory', false) | ||
tr.setMeta('preventUpdate', true) | ||
this.editor.view.dispatch(tr) | ||
} | ||
}, | ||
}) | ||
|
||
export default KeepSyntax |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @copyright Copyright (c) 2022 | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
/** | ||
* Add a mark for keeping special markdown syntax unescaped | ||
* | ||
* @param {object} md Markdown object | ||
*/ | ||
export default function keepSyntax(md) { | ||
const escaped = /((?<=\n)[#\-*+>]|[`*\\~[\]]+)/ | ||
|
||
md.core.ruler.before('text_join', 'tag-markdown-syntax', state => { | ||
const open = new state.Token('keep_md_open', 'span', 1) | ||
open.attrSet('class', 'keep-md') | ||
const close = new state.Token('keep_md_close', 'span', -1) | ||
|
||
for (let i = 0; i < state.tokens.length; i++) { | ||
const block = state.tokens[i] | ||
if (block.type !== 'inline') continue | ||
|
||
for (let j = 0; j < block.children.length; j++) { | ||
const token = block.children[j] | ||
if (token.type === 'text') { | ||
const match = escaped.exec(token.content) | ||
if (match) { | ||
const contentNext = match.index + match[0].length | ||
block.children.splice(j, 1, | ||
Object.assign({}, token, { content: token.content.slice(0, match.index) }), | ||
Object.assign({}, open), | ||
Object.assign({}, token, { content: token.content.slice(match.index, contentNext) }), | ||
Object.assign({}, close), | ||
Object.assign({}, token, { content: token.content.slice(contentNext) }) | ||
) | ||
j += 3 | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false | ||
}) | ||
} |