How to use the unist-util-visit function in unist-util-visit

To help you get started, we’ve selected a few unist-util-visit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jlevy / atom-flowmark / lib / normalize-footnotes.js View on Github external
assert(node.identifier, 'All footnote definitions should have an id');
      const definitionText = nodeText(node);
      footnoteTextSet.add(definitionText);
      const newId = generateFootnoteId(definitionText);
      footnoteIdMapping[node.identifier] = newId;
      newFootnoteIds.add(newId);
      console.log(`Mapping footnote id: ${node.type} ^${node.identifier} -> ^${footnoteIdMapping[node.identifier]}`);
    }
  );

  assert(newFootnoteIds.size === footnoteTextSet.size, 'Footnote hash id collision, which should be very unlikely');

  // Apply the old->new mapping on ids, also dropping duplicate definitions.
  const referencesSeen = new Set();
  const definitionsSeen = new Set();
  visit(
    mdast,
    node => node.type === 'footnoteReference' || node.type === 'footnoteDefinition',
    (node, index, parent) => {
      const newId = footnoteIdMapping[node.identifier];
      if (node.type === 'footnoteReference') {
        if (newId) {
          node.identifier = newId;
        } else {
          missingFootnoteIds.add(node.identifier)
          node.identifier += ".missing";
        }
        footnoteRefCount++;
        referencesSeen.add(node.identifier);
      } else {
        assert(node.type === 'footnoteDefinition');
        assert(newId);
github mdx-js / mdx / src / jsx.js View on Github external
export default options => (tree, file) =>
  visit(tree, 'element', (node, i, parent) => {
    // TODO: walk all opening tags and match with closing
    // and combine into a single `html` node for HAST
    // transformation
  })
github Mr0grog / google-docs-to-markdown / lib / fix-google-html.js View on Github external
export function unInlineStyles (node) {
  visit(node, isStyled, (node, index, parent) => {
    const style = node.properties.style;
    if (/font-style:\s*italic/.test(style)) {
      wrapChildren(node, hast('em'));
    }
    if (/font-weight:\s*(bold|700)/.test(style)) {
      wrapChildren(node, hast('strong'));
    }
  });
}
github wix / okidoc / packages / okidoc-site / site / src / utils / getPageHeadingsAndHtmlAst.js View on Github external
function fixHeadingLinks(htmlAst) {
  const slugger = new Slugger();

  visit(htmlAst, 'element', element => {
    if (HEADING_TAG_PATTERN.test(element.tagName)) {
      const id = element.properties.id;
      const slug = slugger.slug(id);

      if (id !== slug) {
        element.properties.id = slug;
        element.children.forEach(child => {
          if (child.type === 'element' && child.tagName === 'a') {
            child.properties.href = `#${slug}`;
          }
        });
      }
    }
  });
}
github umijs / father-doc / packages / umi-plugin-father-doc / src / transformer / remark / jsx.ts View on Github external
export default () => (ast: Node) => {
  visitParents(ast, 'text', textVisitor);
  visit(ast, 'raw', rawVisitor);
};
github marp-team / marp-vscode / src / diagnostics / deprecated-dollar-prefix.ts View on Github external
visit(parseMd(markdown), 'html', (n: any) =>
    visit(parseHtml(n.value), 'comment', (c: any) => {
      const trimmedLeft = c.value.replace(/^-*\s*/, '')

      detectDirectives(
        trimmedLeft.replace(/\s*-*$/, ''),
        index +
          n.position.start.offset +
          c.position.start.offset +
          4 +
          (c.value.length - trimmedLeft.length)
      )
    })
  )
github andreypopp / reactdown / src / MDAST.js View on Github external
export function renderNodeValue(node: MDASTAnyNode): ?string {
  let value = null;
  visit(node, (node: MDASTAnyNode) => {
    if (node.value) {
      value = value || '';
      value = value + node.value;
    }
  });
  return value;
}
github ada-lovecraft / love-notes / src / index.js View on Github external
function transformer(ast, file) {
    var insertions = []

    visit(ast,'code', (node, index, parent) => {
      const {lang} = node
      const data = node.data || {}
      const cmdstr = lang.split(' ')
      if(cmdstr.length >= 2 && cmdstr[1] === '<3') {
        node.lang = cmdstr[0]
        data.process = false
        let htmlsrc = node.value
        if(node.lang === 'js') {
          htmlsrc = ``
        }
        const htmlNode = u('html', {value: htmlsrc})
        insertions.push({node, index, parent, htmlNode})
      }
      node.data = data
    })
    insertions.forEach((insert, idx) => {
github logux / logux.io / scripts / steps / read-docs.js View on Github external
}
    unistFlatmap(tree, node => {
      if (node.lang === 'sh' && node.value.startsWith('npm ')) {
        return [
          html('<details><summary>npm</summary>'),
          node,
          html('</details>'),
          html('<details><summary>Yarn</summary>'),
          { type: 'code', lang: 'sh', value: npmToYarn(node.value) },
          html('</details>')
        ]
      } else {
        return [node]
      }
    })
    unistVisit(tree, node =&gt; {
      if (node.type === 'heading') {
        if (node.depth === 1) {
          onTitle(node.children[0].value)
        }
      } else if (node.type === 'link' || node.type === 'definition') {
        node.url = node.url
          .replace(/^..\//, '../../')
          .replace(/^.\//, '../')
          .replace(/\.md(#.+)?$/, '/$1')
      }
    })
  }
}

unist-util-visit

unist utility to visit nodes

MIT
Latest version published 10 months ago

Package Health Score

83 / 100
Full package analysis