From 09356d0d92a28a68c38fcb6b98490bb199ea2e5c Mon Sep 17 00:00:00 2001
From: yutent ') && txt.endsWith('
')) {
- txt = trim(txt.slice(5, -6))
+ txt = trim(txt.slice(6, -7))
} else {
txt = trim(txt)
}
diff --git a/src/markd/core.js b/src/markd/core.js
index 4cb4b42..f1f4fc0 100644
--- a/src/markd/core.js
+++ b/src/markd/core.js
@@ -174,19 +174,19 @@ class Tool {
// 初始化字符串, 处理多余换行等
static init(str = '') {
- var links = {}
- var list = []
- var lines = fixed(str).split('\n')
- var isCodeBlock = false // 是否代码块
- var isTable = false // 是否表格
- var emptyLineLength = 0 //连续空行的数量
+ let links = {}
+ let list = []
+ let lines = fixed(str).split('\n')
+ let isCodeBlock = false // 是否代码块
+ let isTable = false // 是否表格
+ let emptyLines = 0 //连续空行的数量
for (let it of lines) {
let tmp = it.trim()
// 非空行
if (tmp) {
- emptyLineLength = 0
+ emptyLines = 0
if (tmp.startsWith('```')) {
if (isCodeBlock) {
list.push('\n\n')
@@ -244,32 +244,35 @@ class Tool {
}
}
} else {
+ if (list.length === 0) {
+ continue
+ }
if (isTable) {
isTable = false
list.push('\n\n')
continue
}
- if (list.length === 0 || (!isCodeBlock && emptyLineLength > 1)) {
- continue
+ if (isCodeBlock || emptyLines < 2) {
+ emptyLines++
+ list.push(tmp)
}
- emptyLineLength++
- list.push(tmp)
}
}
-
return new this(list, links)
}
parse() {
let html = ''
let isCodeBlock = false // 是否代码块
- let emptyLineLength = 0 //连续空行的数量
+ let emptyLines = 0 //连续空行的数量
let isBlockquote = false
let isTable = false
let tableAlign = null
let blockquoteLevel = 0
let isParagraph = false
+ let isQList = false
+ let qListTag = null
let isList = false
let listTagQueue = []
@@ -281,8 +284,10 @@ class Tool {
for (let it of this.list) {
// 非空行
if (it) {
- let lastLineIsEmpty = emptyLineLength > 0
- emptyLineLength = 0
+ // 缓存上一个连续空行数
+ let lastEmptyLines = emptyLines
+
+ emptyLines = 0
if (~it.indexOf('') || ~it.indexOf('
')) {
if (isList) {
@@ -361,7 +366,6 @@ class Tool {
let hrName = Helper.isHr(it)
if (typeof hrName === 'string') {
html += Decoder.hr(hrName)
- emptyLineLength = 0
continue
}
@@ -385,15 +389,7 @@ class Tool {
}
// 有列表也先结束
if (isList) {
- while (listTagQueue.length) {
- let tag = listTagQueue.pop()
- html += `${' '.repeat(
- listTagQueue.length ? listTagQueue.length + 1 : 0
- )}${tag}>\n`
- if (listTagQueue.length) {
- html += `${' '.repeat(listTagQueue.length)}\n`
- }
- }
+ html = this.#closeList(html, listTagQueue, isParagraph)
isList = false
}
isHtmlBlock = false
@@ -411,12 +407,11 @@ class Tool {
// 若之前已经有一个未闭合的引用, 需要减去已有缩进级别, 避免产生新的引用标签
if (isBlockquote) {
loop = len - blockquoteLevel
- } else {
}
while (loop > 0) {
loop--
- tmp += ''
+ tmp += `\n
`
}
blockquoteLevel = len
@@ -424,12 +419,18 @@ class Tool {
return tmp
})
+ if (isParagraph) {
+ isParagraph = false
+ html = trimBr(html)
+ html += '
${it}` + if (lastEmptyLines > 0) { + html = html.replace(/<\/li>\n*?$/, '') + `
${it}` + } else { + html = html.replace(/<\/li>\n*?$/, '') + ` ${it}\n` + continue + } } } else { if (isParagraph) { - html += `${it}\n` + html += ` ${it}\n` } else { html += `\n
${it}` } @@ -590,42 +592,31 @@ class Tool { if (isCodeBlock) { html += it + '\n' } else { - emptyLineLength++ - + emptyLines++ + // // 引用结束 if (isBlockquote) { - if (emptyLineLength > 0) { - isBlockquote = false - emptyLineLength = 0 - while (blockquoteLevel > 0) { - blockquoteLevel-- - html += '\n' - } + if (isQList) { + html += `\n${qListTag}>\n` + isQList = false } - continue + html = this.#closeQuotes(html, blockquoteLevel) + isBlockquote = false + blockquoteLevel = 0 } - if (isList) { - if (emptyLineLength > 1) { - while (listTagQueue.length) { - let tag = listTagQueue.pop() - html += `${' '.repeat( - listTagQueue.length ? listTagQueue.length + 1 : 0 - )}${tag}>\n` - if (listTagQueue.length) { - html += `${' '.repeat(listTagQueue.length)}\n` - } - } - isList = false - emptyLineLength = 0 + if (isList && emptyLines > 1) { + html = this.#closeList(html, listTagQueue, isParagraph) + isList = false + emptyLines = 0 + if (isParagraph) { + isParagraph = false } - continue } - // if (isParagraph) { - if (emptyLineLength > 1) { - emptyLineLength = 0 + if (emptyLines > 1) { + emptyLines = 0 isParagraph = false html = trimBr(html) html += '
\n' @@ -637,6 +628,19 @@ class Tool { } } + // 引用结束 + if (isBlockquote) { + html = this.#closeQuotes(html, blockquoteLevel) + blockquoteLevel = 0 + } + + if (isList) { + html = this.#closeList(html, listTagQueue, isParagraph) + if (isParagraph) { + isParagraph = false + } + } + if (isParagraph) { html += '' } @@ -644,6 +648,29 @@ class Tool { delete this.__LINKS__ return html.trim() } + + #closeList(html, tags, isParagraph) { + if (isParagraph) { + html = trimBr(html) + html += '\n' + } + while (tags.length) { + let tag = tags.pop() + html += `${' '.repeat(tags.length ? tags.length + 1 : 0)}${tag}>\n` + if (tags.length) { + html += `${' '.repeat(tags.length)}\n` + } + } + return html + } + + #closeQuotes(html, level) { + while (level > 0) { + level-- + html += '\n' + } + return html + } } export default function (str) { diff --git a/src/meditor/index.js b/src/meditor/index.js index 4ad1250..5819821 100644 --- a/src/meditor/index.js +++ b/src/meditor/index.js @@ -907,7 +907,9 @@ class MEditor extends Component { } } - mounted() {} + mounted() { + Addon.preview.call(this, this) + } unmounted() {}