|
@@ -208,7 +208,7 @@ const syntax = {
|
|
|
childName: 'unOrderedListItem',
|
|
|
//paired: true,
|
|
|
recursive: true,
|
|
|
- regexp: /\n(\s*)-\s*/,
|
|
|
+ regexp: /-\s*/,
|
|
|
content:{
|
|
|
start:{
|
|
|
point: 'end',
|
|
@@ -227,14 +227,19 @@ const syntax = {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+const indentRegexp = (regexp,count) => new RegExp(`\\n(\s${count === undefined ? '*' : `{${count}}` })` + regexp.toString().slice(1,-1))
|
|
|
+
|
|
|
function findNearest(md, mdTags, offset=0){
|
|
|
let nearest, nearestMatch = {index: Infinity};
|
|
|
for (let [mdTag, {paired,
|
|
|
startRegexp,
|
|
|
- regexp}] of Object.entries(mdTags)) {
|
|
|
+ regexp, indent}] of Object.entries(mdTags)) {
|
|
|
if (mdTag === 'root') continue;
|
|
|
|
|
|
- let match = md.offsetMatch(offset, startRegexp || regexp)
|
|
|
+ regexp = startRegexp || regexp
|
|
|
+ regexp = indent ? indentRegexp(regexp) : regexp
|
|
|
+
|
|
|
+ let match = md.offsetMatch(offset, regexp)
|
|
|
if (match && match.index < nearestMatch.index){
|
|
|
nearestMatch = match
|
|
|
nearest = mdTag
|
|
@@ -259,16 +264,31 @@ Array.prototype.last = function(){
|
|
|
return this[this.length -1]
|
|
|
}
|
|
|
|
|
|
+String.prototype.cutIndent = function(indent){
|
|
|
+ let lines = this.split('\n').map(line => line.slice(0, indent).match(/^\s*$/) ? line.slice(indent) : line)
|
|
|
+ return lines.join('\n')
|
|
|
+}
|
|
|
+
|
|
|
function buildAST(md, mdTags=syntax, offset=0, tree={tag: 'root'}, stack=[]){
|
|
|
const currentNode = stack.last() || tree
|
|
|
if (currentNode.tag === 'root') md = '\n' + md + '\n'
|
|
|
currentNode.children = currentNode.children || []
|
|
|
const { children } = currentNode
|
|
|
|
|
|
- const {indent, title, recursive, endRegexp, content: {end: {offset: offsetEnd, point} }, forward } = mdTags[currentNode.tag]
|
|
|
+ let {indent, title, recursive, regexp, endRegexp, content: {end: {offset: offsetEnd, point} }, forward } = mdTags[currentNode.tag]
|
|
|
|
|
|
if (indent){
|
|
|
- debugger;
|
|
|
+ if (currentNode.parent.tag == currentNode.tag){ //li
|
|
|
+
|
|
|
+ }
|
|
|
+ else { // ul or ol
|
|
|
+ const indentLength = currentNode.startMatch[1].length
|
|
|
+ endRegexp = indentRegexp(regexp, indentLength)
|
|
|
+ let endMatch = md.offsetMatch(offset, endRegexp) || {index: md.length +1, 0: 'zzz'}
|
|
|
+
|
|
|
+ let listMD = md.slice(offset, endMatch.index + offset).cutIndent(currentNode.startMatch[0].length -1)
|
|
|
+ debugger;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (title){
|