How to use unist-util-visit - 10 common examples

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 retextjs / retext-readability / index.js View on Github external
character: letters,
          letter: letters
        }

        report(file, sentence, threshold, targetAge, [
          gradeToAge(daleChallFormula.gradeLevel(daleChallFormula(counts))[1]),
          gradeToAge(ari(counts)),
          gradeToAge(colemanLiau(counts)),
          fleschToAge(flesch(counts)),
          smogToAge(smog(counts)),
          gradeToAge(gunningFog(counts)),
          gradeToAge(spacheFormula(counts))
        ])
      }

      return visit.SKIP

      function visitor(node) {
        var value = toString(node)
        var syllables = syllable(value)

        wordCount++
        totalSyllables += syllables
        letters += value.length
        caseless = value.toLowerCase()

        // Count complex words for gunning-fog based on whether they have three
        // or more syllables and whether they aren’t proper nouns.  The last is
        // checked a little simple, so this index might be over-eager.
        if (syllables >= 3) {
          polysillabicWord++
github retextjs / retext-pos / index.js View on Github external
}
    }

    // Apply tags if there are words.
    if (values.length !== 0) {
      tags = tagger.tag(values)
      length = tags.length
      index = -1

      while (++index < length) {
        patch(words[index], tags[index][1])
      }
    }

    // Don’t enter sentences.
    return visit.SKIP
  }
github retextjs / retext-equality / lib / factory.js View on Github external
search(node, normalize, handle)
        search(node, noNormalize, handle, true)

        // Ignore or emit offending words based on their pattern.
        for (key in matches) {
          type = byId[key].type

          if (type === 'or' && noBinary) {
            type = 'simple'
          }

          handlers[type](matches[key], byId[key], file)
        }

        return visit.SKIP

        // Handle a match.
        function handle(match, position, parent, phrase) {
          var index = list.indexOf(phrase)
          var pattern = byIndex[index]
          var id = pattern.id

          if (phrase !== phrase.toLowerCase() && toString(match) !== phrase) {
            return
          }

          if (!(id in matches)) {
            matches[id] = []
          }

          matches[id].push({
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;
}

unist-util-visit

unist utility to visit nodes

MIT
Latest version published 10 months ago

Package Health Score

80 / 100
Full package analysis