123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /*
- * 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__. ")
|