How to use mdast-util-to-string - 9 common examples

To help you get started, we’ve selected a few mdast-util-to-string 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 netlify / netlify-cms / packages / netlify-cms-widget-markdown / src / serializers / slateRemark.js View on Github external
convertedNodes.push(u(markMap[markType], node.data, node.text));
        } else if (!markType && markNodes.length === 1 && markNodes[0].object === 'inline') {
          const node = markNodes[0];
          convertedNodes.push(convertInlineNode(node, convertInlineAndTextChildren(node.nodes)));
        } else {
          const {
            leadingWhitespace,
            trailingWhitespace,
            centerNodes,
          } = normalizeFlankingWhitespace(markNodes);
          const children = convertInlineAndTextChildren(centerNodes);
          const markNode = u(markMap[markType], children);

          // Filter out empty marks, otherwise their output literally by
          // remark-stringify, eg. an empty bold node becomes "****"
          if (mdastToString(markNode) === '') {
            remainingNodes = remainder;
            continue;
          }
          const createText = text => text && u('html', text);
          const normalizedNodes = [
            createText(leadingWhitespace),
            markNode,
            createText(trailingWhitespace),
          ].filter(val => val);
          convertedNodes.push(...normalizedNodes);
        }
        remainingNodes = remainder;
      } else {
        remainingNodes.shift();
        convertedNodes.push(u('html', nextNode.text));
      }
github andreypopp / mocha-doctest / src / compile.js View on Github external
mdastVisitNode(node, 'code', (node, index, parent) => {
    let prev = parent.children[index - 1];
    let title = null;
    if (prev && prev.type === 'paragraph') {
      title = mdastToString(prev);
    }
    if (title && title[title.length - 1] === ':') {
      title = title.slice(0, title.length - 1);
    }
    if (SUPPORTED_LANG[node.lang]) {
      testCaseList.push({
        title: title,
        lang: node.lang,
        value: node.value
      });
    }
  });
github Symbitic / markbook / src / renderer / remark-deflist.js View on Github external
const isdeflist = (node, i, parent) =>
  i > 0 &&
  /^:\s/.test(toString(node)) &&
  !/^:\s/.test(toString(parent.children[i - 1])) &&
  node.type === 'paragraph' &&
  parent.children[i - 1].type === 'paragraph'
github Symbitic / markbook / src / renderer / remark-deflist.js View on Github external
const isdeflist = (node, i, parent) =>
  i > 0 &&
  /^:\s/.test(toString(node)) &&
  !/^:\s/.test(toString(parent.children[i - 1])) &&
  node.type === 'paragraph' &&
  parent.children[i - 1].type === 'paragraph'
github netlify / netlify-cms / packages / netlify-cms-widget-markdown / src / serializers / slateRemark.js View on Github external
* level of heading is a separately named type. The MDAST schema just
       * has a single "heading" type with the depth stored in a "depth"
       * property on the node. Here we derive the depth from the Slate node
       * type - e.g., for "heading-two", we need a depth value of "2".
       */
      case 'heading-one':
      case 'heading-two':
      case 'heading-three':
      case 'heading-four':
      case 'heading-five':
      case 'heading-six': {
        const depthMap = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 };
        const depthText = node.type.split('-')[1];
        const depth = depthMap[depthText];
        const mdastNode = u(typeMap[node.type], { depth }, children);
        if (mdastToString(mdastNode)) {
          return mdastNode;
        }
        return;
      }

      /**
       * Code Blocks
       *
       * Code block nodes may have a single text child, or instead be void and
       * store their value in `data.code`. They also may have a code language
       * stored in the "lang" data property. Here we transfer both the node value
       * and the "lang" data property to the new MDAST node, and spread any
       * remaining data as `data`.
       */
      case 'code-block': {
        const { lang, code, ...data } = get(node, 'data', {});
github andreypopp / reactdown / src / model / title.js View on Github external
export default function title(node: MDASTRootNode): ?string {
  for (let i = 0; i < node.children.length; i++) {
    let child = node.children[i];
    if (child.type === 'heading') {
      if (child.depth === 1) {
        return nodeToString(child);
      }
    }
  }
  return null;
}
github netlify / netlify-cms / src / components / Widgets / Markdown / unified.js View on Github external
function processShortcodes(node) {
    /**
     * If the node is not eligible to contain a shortcode, return the original
     * node unchanged.
     */
    if (!nodeMayContainShortcode(node)) return node;

    /**
     * Combine the text values of all children to a single string, check the
     * string for a shortcode pattern match, and validate the match.
     */
    const text = mdastToString(node).trim();
    const { plugin, match } = matchTextToPlugin(text);
    const matchIsValid = validateMatch(text, match);

    /**
     * If a valid match is found, return a new node with shortcode data
     * included. Otherwise, return the original node.
     */
    return matchIsValid ? createShortcodeNode(text, plugin, match) : node;
  };
github netlify / netlify-cms / packages / netlify-cms-widget-markdown / src / serializers / remarkPaddedLinks.js View on Github external
function transformChildren(node) {
    if (node.type !== 'link') return node;

    /**
     * Get the node's complete string value, check for leading and trailing
     * whitespace, and get nodes from each edge where whitespace is found.
     */
    const text = toString(node);
    const leadingWhitespaceNode = startsWith(text, ' ') && getEdgeTextChild(node);
    const trailingWhitespaceNode = endsWith(text, ' ') && getEdgeTextChild(node, true);

    if (!leadingWhitespaceNode && !trailingWhitespaceNode) return node;

    /**
     * Trim the edge nodes in place. Unified handles everything in a mutable
     * fashion, so it's often simpler to do the same when working with Unified
     * ASTs.
     */
    if (leadingWhitespaceNode) {
      leadingWhitespaceNode.value = trimStart(leadingWhitespaceNode.value);
    }

    if (trailingWhitespaceNode) {
      trailingWhitespaceNode.value = trimEnd(trailingWhitespaceNode.value);

mdast-util-to-string

mdast utility to get the plain text content of a node

MIT
Latest version published 1 year ago

Package Health Score

71 / 100
Full package analysis

Popular mdast-util-to-string functions