npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

remark-render

v0.0.16

Published

Compile markdown to Virtual DOM with remark.

Downloads

14

Readme

remark-render

Compiles markdown to Virtual DOM. Built on remark, an extensively tested and pluggable markdown processor.

  • [x] Supports raw HTML
  • [x] Support VNode keys
  • [x] Multiple modes and Optionally settings (hyperscript virtual-dom Vue React snabbdom)
  • [x] Custom Renderer / Extend Renderer

Installation

npm:

npm install remark-render

Usage

Say we have the following file, example.js:

var renderer = require('remark-preact-renderer');

unified().use(render, {
  renderer: renderer,
  h: h,
  rootClassName: 'markdown-body',
  rootTagName: 'main'
})

=> <main class="markdown-body"></main>

options

| | remark | | :------| :------ | | renderer | framework renderer | | h | create element function | | rootClassName | vdom root element class name | | rootTagName | vdom root element tag type. default is div |

var unified = require('unified')
var parse = require('remark-parse')
var render = require('remark-render')
  
var h = require('hyperscript');
unified()
  .use(parse)
  .use(render, {
     h: h, // create element function
     rootClassName: 'markdown-body' // vdom root element class name,
     rootTagName: 'main'
  })
  .process('# h1  \n## h2', function(err, file) {
    if (err) throw err
    console.dir(file.contents, {depth: null})
  })
 

Renderers

| | renderers | | :------| ------: | | React | remark-preact-renderer | | Preact | remark-react-renderer | | Vue | remark-vue-renderer | | HyperScript | remark-hyperscript-renderer | | snabbdom | remark-snabbdom-renderer | | virtual-dom | remark-virtual-dom-renderer | | morphdom | morphdom |

Examples

Presets provide a potentially sharable way to render. They can contain multiple modes and optionally settings as well.

html
<div id="preview"></div>
HyperScript
npm install remark-render remark-hyperscript-renderer
var unified = require('unified')
var parse = require('remark-parse')
var render = require('remark-render')
var renderer = require('remark-hyperscript-renderer');

var h = require('hyperscript');
unified()
  .use(parse)
  .use(render, {
     renderer: renderer,
     h: h,
     rootClassName: 'markdown-body'
  })
  .process('# h1  \n## h2', function(err, file) {
    if (err) throw err
    console.dir(file.contents, {depth: null})
    var preview = document.getElementById('preview');
    preview.appendChild(vdom);
  })
 
React
npm install remark-react-renderer
var unified = require('unified')
var parse = require('remark-parse')
var render = require('remark-render')
var renderer = require('remark-react-renderer');
var React = require('react');
var h = React.createElement;

var processor = unified()
    .use(parse)
    .use(render, {
        renderer: renderer,
        h: h
    });
 
var file = processor.processSync('# h1');

ReactDOM.render(
    file.contents,
    document.getElementById('preview')
);
 
Vue
npm install remark-vue-renderer
var unified = require('unified')
var parse = require('remark-parse')
var render = require('remark-render')
var renderer = require('remark-vue-renderer');
var Vue = require('vue');
 
var processor = unified()
    .use(parse)
    .use(render, {
        renderer: renderer
    }).freeze();
 
const app = new Vue({
    el: '#preview',
    render(h) {
        var file = processor().data('h', h).processSync('# h1');
        return file.contents;
    }
}); 
Writing a custom renderer/Extend a renderer
var unified = require('unified')
var parse = require('remark-parse')
var render = require('remark-render')
var renderer = require('remark-hyperscript-renderer'); 
var h = require('hyperscript');

renderer.text = function(h, node, children) {
    return h('span', {
        key: node.properties.key,
        style: {'font-size': '60px'}
    }, node.value);
};

unified()
  .use(parse)
  .use(render, {
     h: h,
     renderer: renderer
  })
  .process('# h1  \n## h2', function(err, file) {
    if (err) throw err
    var preview = document.getElementById('preview');
    preview.appendChild(vdom);
  })
morphdom - Writing a custom renderer.

node MDAST

{
    "type": "heading",
    "depth": 1 <= number <= 6,
    "tagName": "a",
    "parent": parent,
    "properties": {
        "href": "href",
        "id": "id",
        "className": ["bravo"]
    },
    "children": []
}
renderer
{
    /**
     * root element (根元素)
     * @param {*} h create element function (构建元素节点函数)
     * @param {*} node  current node  (当前根元素节点)
     * node.key is node index if node in array for key. default is 0 (如果当前节点在数组中,返回当前节点在数组中的序列,这是为了构建数组key)
     * @param {*} children node create element children (当前节点的子节点)
     */
    root : function(h, node, children) {},

    text : function(h, node, children) {},

    ...
}
var unified = require('unified');
var parse = require('remark-parse');
var render = require('remark-render')
var morphdom = require('morphdom');

function createElement(type, props, children) {
    let dom = document.createElement(type);
    if(props.className) {
        dom.className = props.className;
    }
    if(props.hasOwnProperty('id')) {
        dom.id = props.id;
    }
    dom.appendChild( createElements(children) );
    return dom;
}

function createElements(children) {
    const doms = document.createDocumentFragment();
    children && children.length > 0 && children.forEach(function (dom) {
        dom && doms.appendChild(dom);
    });
    return doms;
}

var renderer = {
    root: function(h, node, children) {
        return h('div', node.props, children);
    },
    text: function(h, node, children) {
        return document.createTextNode(node.value);
    },
    blockquote: function(h, node, children) {
        return h('blockquote', node.props, children);
    },
    heading: function(h, node, children) {
        return h('h'+node.depth, node.props, children);
    }
    ...
}


var processor = unified()
    .use(parse, {})
    .use(render, {
        renderer: renderer,
        h: createElement
    });

var file = processor.processSync('# h1\n## h2');
var markdownContainer = file.contents;

var previewContainer = document.getElementById('preview');

morphdom(previewContainer, markdownContainer);

License

MIT © yucopowo