diff --git a/src/markd/core.js b/src/markd/core.js
index 11abde5..1b749f7 100644
--- a/src/markd/core.js
+++ b/src/markd/core.js
@@ -271,8 +271,7 @@ class Tool {
let isParagraph = false
let isList = false
- let orderListLevel = -1
- let unorderListLevel = -1
+ let listTagQueue = []
let isQuoteList = false // 引用中的列表, 只支持一层级
let quoteListStyle = 0 // 1有序, 2 无序
@@ -282,10 +281,19 @@ class Tool {
for (let it of this.list) {
// 非空行
if (it) {
+ let lastLineIsEmpty = emptyLineLength > 0
emptyLineLength = 0
if (~it.indexOf('
')) {
- html += it
+ if (isList) {
+ if (isTable) {
+ html += it + `${' '.repeat(listTagQueue.length)}\n`
+ } else {
+ html = html.replace(/<\/li>\n*?$/, '') + it
+ }
+ } else {
+ html += it
+ }
isTable = !isTable
tableAlign = true
isHtmlBlock = false
@@ -328,7 +336,15 @@ class Tool {
isParagraph = false
html += '\n'
}
- html += it
+ if (isList) {
+ if (isCodeBlock) {
+ html += it + `${' '.repeat(listTagQueue.length)}\n`
+ } else {
+ html = html.replace(/<\/li>\n*?$/, '') + it
+ }
+ } else {
+ html += it
+ }
isCodeBlock = !isCodeBlock
isHtmlBlock = false
continue
@@ -345,6 +361,7 @@ class Tool {
let hrName = Helper.isHr(it)
if (typeof hrName === 'string') {
html += Decoder.hr(hrName)
+ emptyLineLength = 0
continue
}
@@ -368,13 +385,13 @@ class Tool {
}
// 有列表也先结束
if (isList) {
- while (orderListLevel > -1 || unorderListLevel > -1) {
- if (orderListLevel > unorderListLevel) {
- html += '\n'
- orderListLevel--
- } else {
- html += '\n'
- unorderListLevel--
+ 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
@@ -469,45 +486,47 @@ class Tool {
let tmp = Helper.ltrim(it)
let ltrim = it.length - tmp.length
let word = tmp.replace(LIST_RE, '').trim()
- let level = Math.floor(ltrim / 2) //缩进级别
+ let _level = listTagQueue.length - 1 // 上一个缩进级别
+ let level = Math.floor(ltrim / 2) // 当前缩进级别
let tag = listChecked === 1 ? 'ol' : 'ul'
if (isParagraph) {
isParagraph = false
- html += '\n'
+ if (!isList) {
+ html = trimBr(html)
+ html += '\n'
+ }
}
if (isList) {
- let _level = listChecked === 1 ? orderListLevel : unorderListLevel
-
// 大于上一个缩进
if (level > _level) {
+ listTagQueue.push(tag)
// 需要先把上一个列表的结束符去掉
html = html.trim().replace(/<\/li>$/, '')
- html += `\n<${tag}>\n${word}\n`
+ html += `\n${' '.repeat(level + 1)}<${tag}>\n${' '.repeat(
+ level + 1
+ )}${word}\n`
}
// 跟上一个平级
else if (level === _level) {
- html += `${word}\n`
+ html += `${' '.repeat(level + 1)}${word}\n`
}
// 比上一个缩进要小
else {
- html += `${tag}>\n\n${word}\n`
- }
+ for (let i = _level - level; i > 0; i--) {
+ let _tag = listTagQueue.pop()
- if (listChecked === 1) {
- orderListLevel = level
- } else {
- unorderListLevel = level
+ html += `${' '.repeat(level + i + 1)}${_tag}>\n${' '.repeat(
+ level + i
+ )}\n`
+ }
+
+ html += `${' '.repeat(level + 1)}${word}\n`
}
} else {
- html += `<${tag}>\n`
- if (listChecked === 1) {
- orderListLevel = level
- } else {
- unorderListLevel = level
- }
- html += `${word}\n`
+ listTagQueue.push(tag)
+ html += `<${tag}>\n ${word}\n`
}
isList = true
@@ -521,6 +540,20 @@ class Tool {
continue
}
+ // 列表之后, 有段落时, 先结束列表
+ if (isList && lastLineIsEmpty) {
+ 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
+ }
+
if (isHtmlBlock || isSingleLineHtml) {
if (isParagraph) {
isParagraph = false
@@ -537,12 +570,20 @@ class Tool {
isHtmlBlock = !isHtmlBlock
html += `${it}\n`
} else {
- if (isParagraph) {
- html += `${it}\n`
+ if (isList) {
+ if (isParagraph) {
+ html += it + `\n${' '.repeat(listTagQueue.length)}\n`
+ } else {
+ html = html.replace(/<\/li>\n*?$/, '') + `\n${it}`
+ }
} else {
- html += `\n
${it}`
- isParagraph = true
+ if (isParagraph) {
+ html += `${it}\n`
+ } else {
+ html += `\n
${it}`
+ }
}
+ isParagraph = true
}
} else {
// 如果是在代码中, 直接拼接, 并加上换行
@@ -566,13 +607,13 @@ class Tool {
if (isList) {
if (emptyLineLength > 1) {
- while (orderListLevel > -1 || unorderListLevel > -1) {
- if (orderListLevel > unorderListLevel) {
- html += '\n'
- orderListLevel--
- } else {
- html += '\n'
- unorderListLevel--
+ 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
@@ -596,9 +637,11 @@ class Tool {
}
}
+ console.log('>>>>')
if (isParagraph) {
html += '
'
}
+ console.log(html)
delete this.list
delete this.__LINKS__
return html.trim()
@@ -606,5 +649,5 @@ class Tool {
}
export default function (str) {
- return Tool.init(str).parse() //.replace(/\\/g, '>')
+ return Tool.init(str).parse()
}