瀏覽代碼

+titles md parse

Ivan Asmer 4 年之前
父節點
當前提交
8ebe6581f6
共有 1 個文件被更改,包括 54 次插入13 次删除
  1. 54 13
      index.js

+ 54 - 13
index.js

@@ -50,7 +50,7 @@ const syntax = {
         paired: true,
         recursive: true,
         startRegexp: /\*\S.*/,
-        endRegexp:   /\S\*\W/,
+        endRegexp:   /\S\*[^*]/,
         content: {
             start: {
                 point: 'start',
@@ -128,6 +128,34 @@ const syntax = {
         forward: {
             point: 'end', //start, startEnd, end, endEnd
             offset: 0
+        },
+        title: {
+            index: 1,
+            recursive: true,
+        },
+        onbuild(md, mdTags, buildAST){ //this = {tag: }
+
+        }
+    },
+    code: {
+        paired: true,
+        recursive: false,
+        startRegexp: /`/,
+        endRegexp: /`/,
+        content: {
+            start: {
+                point: 'start',
+                offset: 1
+            },
+            end: {
+                point: 'start',
+                offset: 0
+            }
+        },
+        begin: 0,
+        forward: {
+            point: 'end', //start, startEnd, end, endEnd
+            offset: 1
         }
     }
 }
@@ -166,16 +194,29 @@ Array.prototype.last = function(){
 
 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 { endRegexp, content: {end: {offset: offsetEnd, point} }, forward } = mdTags[currentNode.tag]
+    const {title, recursive, endRegexp, content: {end: {offset: offsetEnd, point} }, forward } = mdTags[currentNode.tag]
+    
+    if (title){
+        const {index, recursive} = title
+        const {[index]: titleContent } = currentNode.startMatch
+        if (recursive){
+            currentNode.title = buildAST(titleContent, mdTags).children
+        }
+        else {
+            currentNode.title = [titleContent]
+        }
+    }
 
     while(offset < md.length){
         const [nearest, nearestMatch] = findNearest(md, mdTags, offset)
-        const endMatch = md.offsetMatch(offset, endRegexp)
-        if (endMatch) {
-            if (!nearest || endMatch.index <= nearestMatch.index){
+        let endMatch = md.offsetMatch(offset, endRegexp)
+        if (!recursive || endMatch) {
+            if (!recursive || !nearest || endMatch.index <= nearestMatch.index ){
+                endMatch = endMatch || {index: md.length - offset, 0: "zzz"}
                 currentNode.endContent = offset + endMatch.index + offsetEnd + (point === 'end' ? endMatch[0].length : 0)
                 children.push(md.slice(offset, currentNode.endContent))
                 offset += endMatch.index + forward.offset + (forward.point === 'endEnd' ? endMatch[0].length : 0)
@@ -206,12 +247,12 @@ function buildAST(md, mdTags=syntax, offset=0, tree={tag: 'root'}, stack=[]){
     return currentNode
 }
 
-const md = 
-`
-# heading1
-какой-то _текст_
-# heading2
-а тут **шо** цикавого?)))
-`;
-console.log( buildAST(md).children)
+//const md = 
+//`
+//# heading1
+//какой-то _текст_
+//# heading2
+//а тут **шо** цикавого?)))
+//`;
+//console.log( buildAST(md).children)