index.js
function parseDocToTree(doc, defaultTitle) {
  // save the parsed tree
  const tree = [];
  const lines = doc.split('\n');
  let line, m;

  // 保存一个文档区域
  let section = null;
  let parentSection = null;
  // if in a ``` block
  let inBlock = false;

  // little trick，如果第一行不是 #，则增加一个 #
  if (!/^#(?!#)/.test(lines[0])) {
    lines.unshift('# ' + defaultTitle);
  }

  while ((line = lines.shift()) !== undefined) {
    m = line && line.match(/^(#+)(?!#)(.*)$/);

    if (!inBlock && m && m.length == 3) {
      section = {
        title: m[2].trim(),
        level: m[1].length,
        parent: null,
        children: []
      };

      while (parentSection && parentSection.level >= section.level) {
        parentSection = parentSection.parent;
      }

      if (parentSection) {
        section.parent = parentSection;
        parentSection.children.push(section);
        parentSection = section;
      } else {
        tree.push(section);
        parentSection = section;
      }
    } else if (section) {
      if (line.indexOf('```') === 0) {
        inBlock = !inBlock;
      }
      section.children.push(line);
    } else {
      throw new Error(`NOT VALID LINE: ${line}`);
    }
  }

  return tree;
}
function parseDocToTree(doc, defaultTitle) {
  // save the parsed tree
  const tree = [];
  const lines = doc.split('\n');
  let line, m;

  // 保存一个文档区域
  let section = null;
  let parentSection = null;
  // if in a ``` block
  let inBlock = false;

  // little trick，如果第一行不是 #，则增加一个 #
  if (!/^#(?!#)/.test(lines[0])) {
    lines.unshift('# ' + defaultTitle);
  }

  while ((line = lines.shift()) !== undefined) {
    m = line && line.match(/^(#+)(?!#)(.*)$/);

    if (!inBlock && m && m.length == 3) {
      section = {
        title: m[2].trim(),
        level: m[1].length,
        parent: null,
        children: []
      };

      while (parentSection && parentSection.level >= section.level) {
        parentSection = parentSection.parent;
      }

      if (parentSection) {
        section.parent = parentSection;
        parentSection.children.push(section);
        parentSection = section;
      } else {
        tree.push(section);
        parentSection = section;
      }
    } else if (section) {
      if (line.indexOf('```') === 0) {
        inBlock = !inBlock;
      }
      section.children.push(line);
    } else {
      throw new Error(`NOT VALID LINE: ${line}`);
    }
  }

  return tree;
}
function parseDocToTree ( doc , defaultTitle ) { // save the parsed tree const tree = [ ] ; const lines = doc . split ( '\n' ) ; let line , m ; // 保存一个文档区域 let section = null ; let parentSection = null ; // if in a ``` block let inBlock = false ; // little trick，如果第一行不是 #，则增加一个 # if ( ! / ^#(?!#) / . test ( lines [ 0 ] ) ) { lines . unshift ( '# ' + defaultTitle ) ; } while ( ( line = lines . shift ( ) ) !== undefined ) { m = line && line . match ( / ^(#+)(?!#)(.*)$ / ) ; if ( ! inBlock && m && m . length == 3 ) { section = { title : m [ 2 ] . trim ( ) , level : m [ 1 ] . length , parent : null , children : [ ] } ; while ( parentSection && parentSection . level >= section . level ) { parentSection = parentSection . parent ; } if ( parentSection ) { section . parent = parentSection ; parentSection . children . push ( section ) ; parentSection = section ; } else { tree . push ( section ) ; parentSection = section ; } } else if ( section ) { if ( line . indexOf ( '```' ) === 0 ) { inBlock = ! inBlock ; } section . children . push ( line ) ; } else { throw new Error ( ` ${ line } ` ) ; } } return tree ; }
parse a md document to an abstract document tree
parse a md document to an abstract document tree