Skip to content

Commit

Permalink
Rewrite module
Browse files Browse the repository at this point in the history
* Refactor code-style;
* Remove superfluous files;
* Remove markdown parsing (remark) from tests;
* Remove support for sanitising (use a rehype plugin instead);
* `rehypeReactComponents` > `components`;
* Remove white-space cleaning (use `rehype-minify-whitespace`
  instead);
* `mocha` > `tape`;
* `eslint`, `jscs` > `xo`;
* Rewrite documentation.
  • Loading branch information
wooorm committed Oct 22, 2016
1 parent 1e42108 commit 395cf25
Show file tree
Hide file tree
Showing 58 changed files with 281 additions and 1,504 deletions.
8 changes: 1 addition & 7 deletions .editorconfig
Expand Up @@ -2,14 +2,8 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{html,json,remarkrc,eslintrc,sh}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

9 changes: 0 additions & 9 deletions .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
140 changes: 0 additions & 140 deletions .jscs.json

This file was deleted.

1 change: 0 additions & 1 deletion .remarkignore

This file was deleted.

10 changes: 0 additions & 10 deletions .remarkrc

This file was deleted.

1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- 'lts/*'
- 'stable'
sudo: false
28 changes: 0 additions & 28 deletions history.md

This file was deleted.

120 changes: 43 additions & 77 deletions index.js
@@ -1,100 +1,66 @@
'use strict';

/*
* Dependencies.
*/

var sanitize = require('hast-util-sanitize');
/* Dependencies. */
var has = require('has');
var toH = require('hast-to-hyperscript');

try {
var globalCreateElement = require('react').createElement;
} catch (e) { }
/* Expose `rehype-react`. */
module.exports = rehype2react;

var own = {}.hasOwnProperty;
/* Get React’s `createElement`. */
var globalCreateElement;

var TABLE_ELEMENTS = ['table', 'thead', 'tbody', 'tfoot', 'tr'];
try {
/* eslint-disable import/no-extraneous-dependencies */
globalCreateElement = require('react').createElement;
} catch (err) {}

/**
* Attach a react compiler.
*
* @param {Unified} processor - Instance.
* @param {Object?} [options]
* @param {Object?} [options.sanitize]
* - Sanitation schema.
* @param {Object?} [options.rehypeReactComponents]
* @param {Object?} [options.components]
* - Components.
* @param {string?} [options.prefix]
* - Key prefix.
* @param {Function?} [options.createElement]
* - `h()`.
*/
function plugin(processor, options) {
var settings = options || {};
var createElement = settings.createElement || globalCreateElement;
var components = settings.rehypeReactComponents || {};
var clean = settings.sanitize !== false;
var scheme = clean && (typeof settings.sanitize !== 'boolean') ? settings.sanitize : null;

/**
* Wrapper around `createElement` to pass
* components in.
*
* @param {string} name - Element name.
* @param {Object} props - Attributes.
* @return {ReactElement} - React element.
*/
function h(name, props, children) {
var component = own.call(components, name) ? components[name] : name;

/*
* Currently, a warning is triggered by react for
* *any* white-space in tables. So we remove the
* pretty lines for now:
* https://github.com/facebook/react/pull/7081
*/
if (children && TABLE_ELEMENTS.indexOf(component) !== -1) {
children = children.filter(function (child) {
return child !== '\n';
});
}

return createElement(component, props, children);
}

/**
* Extensible constructor.
*/
function Compiler() {}

/**
* Compile MDAST to React.
*
* @param {Node} node - MDAST node.
* @return {ReactElement} - React element.
*/
function compile(node) {
var hast = {
type: 'element',
tagName: 'div',
properties: {},
children: node.children
function rehype2react(processor, options) {
var settings = options || {};
var createElement = settings.createElement || globalCreateElement;
var components = settings.components || {};

Compiler.prototype.compile = compile;

processor.Compiler = Compiler;

return;

function Compiler() {}

/* Compile HAST to React. */
function compile(node) {
if (node.type === 'root') {
if (node.children.length === 1 && node.children[0].type === 'element') {
node = node.children[0];
} else {
node = {
type: 'element',
tagName: 'div',
properties: {},
children: node.children
};

if (clean) {
hast = sanitize(hast, scheme);
}

return toH(h, hast, settings.prefix);
}
}

Compiler.prototype.compile = compile;
return toH(h, node, settings.prefix);
}

processor.Compiler = Compiler;
/* Wrap `createElement` to pass components in. */
function h(name, props, children) {
var component = has(components, name) ? components[name] : name;
return createElement(component, props, children);
}
}

/*
* Expose `plugin`.
*/

module.exports = plugin;

0 comments on commit 395cf25

Please sign in to comment.