From 09356d0d92a28a68c38fcb6b98490bb199ea2e5c Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 28 Sep 2023 18:21:19 +0800 Subject: [PATCH] =?UTF-8?q?mark=E8=A7=A3=E6=9E=90=E5=99=A8=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/code/index.js | 6 +- src/markd/core.js | 185 +++++++++++++++++++++++++------------------ src/meditor/index.js | 4 +- 3 files changed, 112 insertions(+), 83 deletions(-) diff --git a/src/code/index.js b/src/code/index.js index 5d275d3..377aa1e 100644 --- a/src/code/index.js +++ b/src/code/index.js @@ -23,7 +23,7 @@ class Code extends Component { default: '', attribute: false, observer(v) { - this.setCode(v.trim()) + this.setCode(v) } }, lang: '' @@ -232,11 +232,11 @@ class Code extends Component { txt = txt.trim().replace(/^[\r\n]|\s{2,}$/g, '') if (txt.startsWith('') && txt.endsWith('')) { - txt = txt.slice(5, -6).trim() + txt = txt.slice(6, -7) } else if (this.firstElementChild?.tagName === 'TEXTAREA') { txt = this.firstElementChild.value.trim() } else if (txt.startsWith('
') && 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 - )}\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 += '

\n' + } + if (isBlockquote) { // 没有新的缩进引用时, 才添加换行 if (innerQuote) { // 之前有引用的列表时, 直接结束列表 if (isQuoteList) { - html += `\n` + html += `\n` isQuoteList = false } } @@ -448,16 +449,18 @@ class Tool { if (isQuoteList === false) { isQuoteList = true if (currListStyle === 1) { - qlist += '
    ' + qListTag = 'ol' } else { - qlist += '
      ' + qListTag = 'ul' } + qlist += `\n <${qListTag}>` } quoteListStyle = currListStyle qlist += `
    • ${tmp3}
    • ` html += tmp1 + qlist + isQList = true } else { if (innerQuote === false) { html += '
      ' @@ -492,10 +495,8 @@ class Tool { if (isParagraph) { isParagraph = false - if (!isList) { - html = trimBr(html) - html += '

      \n' - } + html = trimBr(html) + html += '

      \n' } if (isList) { @@ -536,22 +537,12 @@ class Tool { // 无"> "前缀的引用, 继续拼到之前的, 并且不换行 if (isBlockquote) { - html += it - continue - } - - // 列表之后, 有段落时, 先结束列表 - if (isList && lastLineIsEmpty) { - while (listTagQueue.length) { - let tag = listTagQueue.pop() - html += `${' '.repeat( - listTagQueue.length ? listTagQueue.length + 1 : 0 - )}\n` - if (listTagQueue.length) { - html += `${' '.repeat(listTagQueue.length)}\n` - } + if (isQList) { + html = html.replace(/<\/li>\n*?$/, '') + ` ${it}` + } else { + html += ' ' + it } - isList = false + continue } if (isHtmlBlock || isSingleLineHtml) { @@ -572,13 +563,24 @@ class Tool { } else { if (isList) { if (isParagraph) { - html += it + `

      \n${' '.repeat(listTagQueue.length)}\n` + if (lastEmptyLines > 0) { + html = trimBr(html) + html += `
      ${it}` + continue + } else { + html += ` ${it}` + } } else { - html = html.replace(/<\/li>\n*?$/, '') + `\n

      ${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\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 - )}\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)}\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() {}