How to use hast-util-to-html - 9 common examples

To help you get started, we’ve selected a few hast-util-to-html 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
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)
        }
      }
    }))
    return html

    function handleLink(h, node) {
      const { uri, desc } = node
github umijs / father-doc / packages / umi-plugin-father-doc / src / transformer / remark / previewer.ts View on Github external
function visitor(node, i, parent) {
  if (node.tagName === 'div' && node.properties?.type === 'previewer') {
    const raw = node.children?.[0]?.value;
    const jsx = (node.children?.[1] && toHtml(node.children?.[1])) || undefined;
    const tsx = (node.children?.[2] && toHtml(node.children?.[2])) || undefined;
    const yaml = node.properties?.meta || {};
    let transformCode = raw;

    // use import way rather than source code way for external demo (for HMR & sourcemap)
    if (node.properties.filePath) {
      transformCode = `
import Demo from '${node.properties.filePath}';
export default () => ;`
    }

    const code = transformer(transformCode, Boolean(tsx));

    // replace original node
    parent.children[i] = {
      previewer: true,
github umijs / father-doc / packages / umi-plugin-father-doc / src / transformer / remark / previewer.ts View on Github external
function visitor(node, i, parent) {
  if (node.tagName === 'div' && node.properties?.type === 'previewer') {
    const raw = node.children?.[0]?.value;
    const jsx = (node.children?.[1] && toHtml(node.children?.[1])) || undefined;
    const tsx = (node.children?.[2] && toHtml(node.children?.[2])) || undefined;
    const yaml = node.properties?.meta || {};
    let transformCode = raw;

    // use import way rather than source code way for external demo (for HMR & sourcemap)
    if (node.properties.filePath) {
      transformCode = `
import Demo from '${node.properties.filePath}';
export default () => ;`
    }

    const code = transformer(transformCode, Boolean(tsx));

    // replace original node
    parent.children[i] = {
      previewer: true,
      type: 'raw',
github Enalean / tuleap / plugins / testmanagement / scripts / testmanagement / src / execution / truncate.js View on Github external
counter = max_length;

        return Object.assign({}, node, {
            value: node.value.substr(0, nb_addable) + "…",
        });
    });

    remove(truncated_tree, (node) => {
        return typeof node.type === "undefined";
    });

    if (images_counter > 0 && toString(truncated_tree).trim().length === 0) {
        return "<p><i>" + placeholder_image_text + "</i></p>";
    }

    return toHtml(truncated_tree);
}
github frontarm / demoboard / packages / demoboard-worker / src / transforms / mdx / rehype-prism.ts View on Github external
let result = node
    try {
      parent.properties.className = (parent.properties.className || []).concat(
        'language-' + lang,
      )
      result = refractor.highlight(nodeToString(node), lang)
    } catch (err) {
      if (/Unknown language/.test(err.message)) {
        return
      }
      throw err
    }

    node.children = []
    node.properties.dangerouslySetInnerHTML = {
      __html: nodeToHTML({
        type: 'root',
        children: result,
      }),
    }
  }
}
github callstack / component-docs / src / utils / rehypePrism.js View on Github external
parent.properties.className = (parent.properties.className || []).concat(
        `language-${lang}`
      );

      result = highlight(nodeToString(node), lang);
    } catch (err) {
      if (/Unknown language/.test(err.message)) {
        return;
      }

      throw err;
    }

    node.children = [];
    node.properties.dangerouslySetInnerHTML = {
      __html: nodeToHTML({
        type: 'root',
        children: result,
      }),
    };
  }
}
github egoist / magi-deprecated / src / lib / index.js View on Github external
function nodeToHtml(node) {
  const hast = toHAST(node)
  return hast ? toHTML(hast) : ''
}
github orgapp / orgajs / www / src / pages / index.js View on Github external
export default () =&gt; {
  const [text, setText] = useState('')
  const oast = parse(text)
  const hast = toHAST(oast)
  const html = stringify(hast)

  return (
    
      
        
          <input value="{text}" rows="10"> setText(e.target.value)}/&gt;
        
        
          
        
      
      <h1>OAST</h1>
      <p>Org Abstract Syntax Tree</p>
github ben-eb / midas / src / index.js View on Github external
const stringify = (root, builder) => {
            builder(toHTML(this._vdom.getContent(root)));
        };
        return {stringify};

hast-util-to-html

hast utility to serialize to HTML

MIT
Latest version published 2 months ago

Package Health Score

86 / 100
Full package analysis

Popular hast-util-to-html functions