How to use the markdown-it/lib/token function in markdown-it

To help you get started, we’ve selected a few markdown-it 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 medfreeman / markdown-it-toc-and-anchor / src / index.js View on Github external
const renderAnchorLink = (anchor, options, tokens, idx) => {
  const attrs = [];

  if (options.anchorClassName != null) {
    attrs.push(["class", options.anchorClassName]);
  }

  attrs.push(["href", `#${anchor}`]);

  const openLinkToken = {
    ...new Token("link_open", "a", 1),
    attrs
  };
  const closeLinkToken = new Token("link_close", "a", -1);

  if (options.wrapHeadingTextInAnchor) {
    tokens[idx + 1].children.unshift(openLinkToken);
    tokens[idx + 1].children.push(closeLinkToken);
  } else {
    const linkTokens = [
      openLinkToken,
      ...renderAnchorLinkSymbol(options),
      closeLinkToken
    ];

    // `push` or `unshift` according to anchorLinkBefore option
    // space is at the opposite side.
    const actionOnArray = {
      false: "push",
      true: "unshift"
github medfreeman / markdown-it-toc-and-anchor / src / index.js View on Github external
const renderAnchorLink = (anchor, options, tokens, idx) => {
  const attrs = [];

  if (options.anchorClassName != null) {
    attrs.push(["class", options.anchorClassName]);
  }

  attrs.push(["href", `#${anchor}`]);

  const openLinkToken = {
    ...new Token("link_open", "a", 1),
    attrs
  };
  const closeLinkToken = new Token("link_close", "a", -1);

  if (options.wrapHeadingTextInAnchor) {
    tokens[idx + 1].children.unshift(openLinkToken);
    tokens[idx + 1].children.push(closeLinkToken);
  } else {
    const linkTokens = [
      openLinkToken,
      ...renderAnchorLinkSymbol(options),
      closeLinkToken
    ];

    // `push` or `unshift` according to anchorLinkBefore option
    // space is at the opposite side.
github egoist / docute / src / utils / markdown-it / headings.js View on Github external
depth,
          slug,
          content,
          html,
          text: html.replace(/<(?:.|\n)*?>/gm, '')
        })
      }

      const children = tokens[idx + 1].children
      tokens[idx + 1].children = [
        {
          ...new Token('link_open', 'router-link', 1),
          attrs: [['class', 'docute-heading-anchor'], [':to', `{hash:'${slug}'}`]]
        },
        ...children,
        new Token('link_close', 'router-link', -1)
      ]

      return self.renderToken(...args)
    }
  }
github medfreeman / markdown-it-toc-and-anchor / src / index.js View on Github external
const space = () => {
  return { ...new Token("text", "", 0), content: " " };
};
github medfreeman / markdown-it-toc-and-anchor / src / index.js View on Github external
const renderAnchorLinkSymbol = options => {
  if (options.anchorLinkSymbolClassName) {
    return [
      {
        ...new Token("span_open", "span", 1),
        attrs: [["class", options.anchorLinkSymbolClassName]]
      },
      {
        ...new Token("text", "", 0),
        content: options.anchorLinkSymbol
      },
      new Token("span_close", "span", -1)
    ];
  } else {
    return [
      {
        ...new Token("text", "", 0),
        content: options.anchorLinkSymbol
      }
    ];
  }
};
github medfreeman / markdown-it-toc-and-anchor / src / index.js View on Github external
const renderAnchorLinkSymbol = options => {
  if (options.anchorLinkSymbolClassName) {
    return [
      {
        ...new Token("span_open", "span", 1),
        attrs: [["class", options.anchorLinkSymbolClassName]]
      },
      {
        ...new Token("text", "", 0),
        content: options.anchorLinkSymbol
      },
      new Token("span_close", "span", -1)
    ];
  } else {
    return [
      {
        ...new Token("text", "", 0),
        content: options.anchorLinkSymbol
      }
    ];
  }
github egoist / docute / src / utils / markdown-it / headings.js View on Github external
if (depth >= 2 && depth <= maxDepth) {
        const html = self.render(tokens[idx + 1].children, options, env)
        state.headings.push({
          depth,
          slug,
          content,
          html,
          text: html.replace(/<(?:.|\n)*?>/gm, '')
        })
      }

      const children = tokens[idx + 1].children
      tokens[idx + 1].children = [
        {
          ...new Token('link_open', 'router-link', 1),
          attrs: [['class', 'docute-heading-anchor'], [':to', `{hash:'${slug}'}`]]
        },
        ...children,
        new Token('link_close', 'router-link', -1)
      ]

      return self.renderToken(...args)
    }
  }