/* * md ast - pluggable markdown parser */ const syntax = { bold: { paired: true, recursive: true, startRegexp: /\s\*\*\S.*/, endRegexp: /\S\*\*\s/, content: { start: { point: 'start', offset: 3 }, end: { point: 'end', offset: 1 } }, forward: { point: 'endEnd', //start, startEnd, end, endEnd offset: -1 } }, bold2: { paired: true, recursive: true, startRegexp: /__\S.*/, endRegexp: /\S__\s/, content: { start: { point: 'start', offset: 3 }, end: { point: 'end', offset: 1 } }, forward: { point: 'endEnd', //start, startEnd, end, endEnd offset: -1 } }, italic: { paired: true, recursive: true, startRegexp: /\s\*\S.*/, endRegexp: /\S\*\s/, content: { start: { point: 'start', offset: 2 }, end: { point: 'end', offset: 1 } }, forward: { point: 'endEnd', //start, startEnd, end, endEnd offset: -1 } }, italic2: { paired: true, recursive: true, startRegexp: /_\S.*/, endRegexp: /\S_\s/, content: { start: { point: 'start', offset: 2 }, end: { point: 'end', offset: 1 } }, forward: { point: 'endEnd', //start, startEnd, end, endEnd offset: -1 } }, } function findNearest(md, mdTags){ let nearest, nearestMatch = {index: Infinity}; for (let [mdTag, {paired, recursive, startRegexp, endRegexp, content: {start, end}, forward}] of Object.entries(mdTags)) { let match = md.match(startRegexp) if (match && match.index < nearestMatch.index){ nearestMatch = match nearest = mdTag } } return [nearest, nearestMatch] } function buildAST(md, mdTags=syntax, tree=[], path=[]){ console.log(findNearest(md, mdTags)) } buildAST("I just __love__ __bold text__. ")