feat: paser makdown

This commit is contained in:
yanmao 2021-11-29 01:17:55 +08:00
parent a68316523a
commit d390fa7463
4 changed files with 49 additions and 32 deletions

View File

@ -387,34 +387,48 @@ class NativeEvent {
});
});
const pasteMarkdown = async (html: string, text: string) => {
// 先解析html
let parser = new Parser(html, this.engine);
const schema = this.engine.schema.clone();
//转换Text没那么严格加入以下规则以免被过滤掉并且 div后面会加换行符
schema.add([
{
name: 'span',
type: 'mark',
},
{
name: 'div',
type: 'block',
},
]);
// 不遍历卡片,不对 ol 节点格式化以免复制列表就去提示检测到markdown
let parserText = parser.toText(schema, false, false);
// html中没有解析到文本
if (!parserText) {
parser = new Parser(text, this.engine);
parserText = parser.toText(schema);
}
const textNode = $(document.createTextNode(parserText));
// 如果没有符合的语法就返回
const parserMarkdown = (text: string) => {
const textNode = $(document.createTextNode(text));
const result = this.engine.trigger(
'paste:markdown-check',
textNode,
);
return {
node: textNode,
result,
};
};
const pasteMarkdown = async (html: string, text: string) => {
// 先解析text
let { node, result } = parserMarkdown(text);
// 没有 markdown尝试解析 html
if (result !== false) {
// 先解析html
let parser = new Parser(html, this.engine);
const schema = this.engine.schema.clone();
//转换Text没那么严格加入以下规则以免被过滤掉并且 div后面会加换行符
schema.add([
{
name: 'span',
type: 'mark',
},
{
name: 'div',
type: 'block',
},
]);
// 不遍历卡片,不对 ol 节点格式化以免复制列表就去提示检测到markdown
let parserText = parser.toText(schema, false, false);
// html中没有解析到文本
if (!parserText) {
parser = new Parser(text, this.engine);
parserText = parser.toText(schema);
}
const htmlResult = parserMarkdown(parserText);
node = htmlResult.node;
result = htmlResult.result;
}
if (result !== false) return;
// 提示是否要转换
this.engine
@ -423,19 +437,21 @@ class NativeEvent {
)
.then(() => {
change.cacheRangeBeforeCommand();
this.engine.trigger('paste:markdown-before', textNode);
this.engine.trigger('paste:markdown', textNode);
this.engine.trigger('paste:markdown-after', textNode);
this.engine.trigger('paste:markdown-before', node);
this.engine.trigger('paste:markdown', node);
this.engine.trigger('paste:markdown-after', node);
textNode.get<Text>()?.normalize();
node.get<Text>()?.normalize();
this.paste(
textNode.text(),
node.text(),
this.#lastePasteRange,
undefined,
false,
);
})
.catch(() => {});
.catch((err) => {
if (err) this.engine.messageError(err);
});
};
change.event.onPaste((data) => {

View File

@ -245,7 +245,7 @@ export default class extends Plugin<Options> {
mode: string = 'text',
) => {
//获取中间字符
const codeText = new Parser(nodes.join('\n'), this.editor).toText();
const codeText = nodes.join('\n');
let code = unescape(codeText);
if (code.endsWith('\n')) code = code.substr(0, code.length - 2);

View File

@ -246,7 +246,7 @@ export default class extends Plugin<Options> {
mode: string = 'text',
) => {
//获取中间字符
const codeText = new Parser(nodes.join('\n'), this.editor).toText();
const codeText = nodes.join('\n');
let code = unescape(codeText);
if (code.endsWith('\n')) code = code.substr(0, code.length - 2);

View File

@ -178,6 +178,7 @@ class Toolbar {
const name = !href || forceEdit ? 'am-link-editor' : 'am-link-preview';
if (this.vm && this.vm._component.name === name) {
if (!this.root || !this.target) return;
this.position?.destroy();
this.position?.bind(this.root, this.target);
return;
} else if (this.vm) {