How to use unist-util-map - 8 common examples

To help you get started, we’ve selected a few unist-util-map 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 orgapp / orgajs / packages / gatsby-transformer-orga / src / extend-node-type.ts View on Github external
async function getHTML(orgContentNode) {
    let body = await getAST({ node: orgContentNode, cache })
    if (body.type === `section`) {
      body = { ...body, children: body.children.slice(1) }
    }
    const filesToCopy = new Map()
    const highlight = pluginOptions.noHighlight !== true
    const handlers = { link: handleLink }

    // offset the levels
    const firstHeadline = select('headline', body)
    const offset = firstHeadline ? firstHeadline.level - 1 : 0
    if (offset > 0) {
      body = map(body, node => {
        if (node.type !== `headline`) return node
        return { ...node, level: node.level - offset }
      })
    }

    const hast = toHAST(body, { highlight, handlers })
    const html = hastToHTML(hast, { allowDangerousHTML: true })
    await Promise.all(Array.from(filesToCopy, async ([linkPath, newFilePath]) => {
      // Don't copy anything is the file already exists at the location.
      if (!fsExtra.existsSync(newFilePath)) {
        try {
          await fsExtra.ensureDir(dirname(newFilePath))
          await fsExtra.copy(linkPath, newFilePath)
        } catch (err) {
          console.error(`error copying file`, err)
        }
github graphql-rules / graphql-rules / src / components / rule.tsx View on Github external
function pullOutCardLinks(ast: any): { ast: any; cardLinksAst: any } {
  // clone ast via `unistMap` before modifying it
  // otherwise when user returns back to the current page
  // the modified ast will not contain card-links
  const result = { ast: unistMap(ast, (n) => n), cardLinksAst: null };

  let pullCardLinks = false;
  unistVisit(result.ast, (node: any, index: number, parent: any) => {
    // find comment <-- card-links --> in markdown
    if (node.type === 'comment' && /card\-links/.test(node.value)) {
      pullCardLinks = true;
    }

    // pull out first <ul> from ast after &lt;-- card-links --&gt;
    if (pullCardLinks &amp;&amp; node.tagName === 'ul') {
      pullCardLinks = false;
      parent.children.splice(index, 1);
      result.cardLinksAst = cleanupListNode(node);
    }
  });
</ul>
github seikichi / restructured / test / TestUtils.js View on Github external
export function assertNode(input, children) {
  const actual = map(new Elements.Document({ children }),
                     node => _.omit(node, ['position', 'blanklines']));
  assert.deepStrictEqual(RST.parse(input), actual);
}
github Enalean / tuleap / plugins / testmanagement / scripts / testmanagement / src / execution / truncate.js View on Github external
export function truncateHTML(content, max_length, placeholder_image_text) {
    const tree = unified().use(parse, { fragment: true }).parse(content);

    let counter = 0;
    let images_counter = 0;
    const truncated_tree = map(tree, function (node) {
        if (counter &gt;= max_length) {
            return null;
        }

        if (node.type === "element" &amp;&amp; node.tagName === "img") {
            images_counter++;
            return null;
        }

        if (node.type !== "text") {
            return node;
        }

        if (counter + node.value.length &lt; max_length) {
            counter += node.value.length;
            return node;
github seikichi / restructured / src / RST.js View on Github external
parse(s, options = {}) {
    const tree = parser.parse(s);

    return map(tree, node => {
      const omits = [];
      if (!options.position) {
        omits.push('position');
      }
      if (!options.blanklines) {
        omits.push('blanklines');
      }
      if (!options.indent) {
        omits.push('indent');
      }
      return _.omit(node, omits);
    });
  },
};
github rstacruz / devhints-engine / packages / rehype-table-separators / index.js View on Github external
function apply(root, options) {
  const opts = { ...DEFAULTS, ...options }

  return map(root, (node, _, parent) => {
    if (isSeparatorRow(node)) {
      return addClassNames(node, [opts.separatorClass])
    }
    if (isSeparatorTD(parent)) {
      return { ...node, value: '' }
    }

    return node
  })
}
github tracespace / tracespace / packages / renderer / src / index.ts View on Github external
export function renderLayer(layer: Layer): SvgLayer {
  const svgRender = unistMap(layer.image, mapImageTreeToSvg)

  if (svgRender.properties) {
    const {width, height} = svgRender.properties
    if (typeof width === 'number') {
      svgRender.properties.width = `${width}${layer.units}`
    }
    if (typeof height === 'number') {
      svgRender.properties.height = `${height}${layer.units}`
    }
  }

  return {...layer, svgRender}
}
github smallstep / hello-mtls / src / ContentBlock.jsx View on Github external
return tree => {
    map(tree, node => {
      if ('value' in node) {
        node.value = parseTemplate(node.value, data);
      }
    });
    return tree;
  };
};

unist-util-map

unist utility to create a new tree by mapping all nodes

MIT
Latest version published 10 months ago

Package Health Score

64 / 100
Full package analysis

Popular unist-util-map functions