Skip to content

Commit

Permalink
Update for [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Feb 23, 2017
1 parent fcfec24 commit 0a27699
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 48 deletions.
21 changes: 6 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,22 @@ var sanitize = require('hast-util-sanitize');

module.exports = plugin;

function plugin(processor, options) {
function plugin(options) {
var settings = options || {};
var clean = settings.sanitize;
var schema = clean && typeof clean === 'object' ? clean : null;
var handlers = settings.handlers || {};

Compiler.prototype.compile = compile;
this.Compiler = compiler;

processor.Compiler = Compiler;

function Compiler(file) {
/* istanbul ignore if - [email protected] */
if (file.extension) {
file.move({extension: 'html'});
}
function compiler(node, file) {
var root = node && node.type && node.type === 'root';
var hast = toHAST(node, {allowDangerousHTML: !clean, handlers: handlers});
var result;

if (file.extname) {
file.extname = '.html';
}
}

function compile(node) {
var root = node && node.type && node.type === 'root';
var hast = toHAST(node, {allowDangerousHTML: !clean, handlers: handlers});
var result;

if (clean) {
hast = sanitize(hast, schema);
Expand Down
25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@
"compile",
"remark"
],
"dependencies": {
"hast-util-sanitize": "^1.0.0",
"hast-util-to-html": "^3.0.0",
"mdast-util-to-hast": "^2.1.1",
"xtend": "^4.0.1"
},
"repository": {
"type": "git",
"url": "https://github.com/wooorm/remark-html.git"
},
"repository": "https://github.com/wooorm/remark-html",
"bugs": "https://github.com/wooorm/remark-html/issues",
"author": "Titus Wormer <[email protected]> (http://wooorm.com)",
"contributors": [
Expand All @@ -28,23 +19,29 @@
"files": [
"index.js"
],
"dependencies": {
"hast-util-sanitize": "^1.0.0",
"hast-util-to-html": "^3.0.0",
"mdast-util-to-hast": "^2.1.1",
"xtend": "^4.0.1"
},
"devDependencies": {
"browserify": "^14.0.0",
"commonmark.json": "^0.27.0",
"esmangle": "^1.0.0",
"is-hidden": "^1.0.1",
"nyc": "^10.0.0",
"remark": "^6.0.0",
"remark": "^7.0.0",
"remark-cli": "^2.0.0",
"remark-github": "^6.0.1",
"remark-github": "^7.0.0",
"remark-preset-wooorm": "^1.0.0",
"remark-toc": "^3.1.0",
"remark-toc": "^4.0.0",
"tape": "^4.0.0",
"to-vfile": "^2.0.0",
"xo": "^0.17.1"
},
"scripts": {
"build-md": "remark *.md doc/*.md --quiet --frail",
"build-md": "# remark *.md doc/*.md --quiet --frail",
"build-bundle": "browserify index.js --bare -s remarkHTML > remark-html.js",
"build-mangle": "esmangle remark-html.js > remark-html.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm install remark-html
var remark = require('remark');
var html = require('remark-html');

var file = remark().use(html).process([
var file = remark().use(html).processSync([
'# Hello & World',
'',
'**Alpha**, _bravo_, and ~~Charlie~~.'
Expand Down Expand Up @@ -60,7 +60,7 @@ var github = require('hast-util-sanitize/lib/github');

var schema = merge(github, {attributes: {'*': ['className']}});

remark().use(html, {sanitize: schema}).process(/*...*/);
remark().use(html, {sanitize: schema}).processSync(/*...*/);
```

## CommonMark
Expand Down
35 changes: 18 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test('remark-html()', function (t) {
t.equal(typeof html, 'function', 'should be a function');

t.doesNotThrow(function () {
html(remark());
html.call(remark());
}, 'should not throw if not passed options');

t.throws(
Expand Down Expand Up @@ -178,7 +178,7 @@ test('remark-html()', function (t) {
}});

t.equal(
processor.process('paragraph text').toString(),
processor.processSync('paragraph text').toString(),
'<p>changed</p>\n',
'should allow overriding handlers'
);
Expand All @@ -194,7 +194,7 @@ test('remark-html()', function (t) {
.use(html);

t.equal(
processor.process('![hello](example.jpg "overwritten")').toString(),
processor.processSync('![hello](example.jpg "overwritten")').toString(),
'<p><img src="example.jpg" alt="hello" title="overwrite"></p>\n',
'should patch and merge attributes'
);
Expand All @@ -208,7 +208,7 @@ test('remark-html()', function (t) {
.use(html);

t.equal(
processor.process('**Bold!**').toString(),
processor.processSync('**Bold!**').toString(),
'<p><b>Bold!</b></p>\n',
'should overwrite a tag-name'
);
Expand All @@ -231,7 +231,7 @@ test('remark-html()', function (t) {
.use(html);

t.equal(
processor.process('`var`').toString(),
processor.processSync('`var`').toString(),
'<p><code><span class="token">var</span></code></p>\n',
'should overwrite content'
);
Expand All @@ -254,7 +254,7 @@ test('remark-html()', function (t) {
.use(html, {sanitize: true});

t.equal(
processor.process('`var`').toString(),
processor.processSync('`var`').toString(),
'<p><code>var</code></p>\n',
'should not overwrite content in `sanitize` mode'
);
Expand All @@ -270,45 +270,45 @@ test('remark-html()', function (t) {
.use(html);

t.equal(
processor.process('```js\nvar\n```\n').toString(),
processor.processSync('```js\nvar\n```\n').toString(),
'<pre><code class="foo">var\n</code></pre>\n',
'should overwrite classes on code'
);

t.equal(
remark().use(html).process('## Hello <span>world</span>').toString(),
remark().use(html).processSync('## Hello <span>world</span>').toString(),
'<h2>Hello <span>world</span></h2>\n',
'should be `sanitation: false` by default'
);

t.equal(
remark().use(html, {
sanitize: true
}).process('## Hello <span>world</span>').toString(),
}).processSync('## Hello <span>world</span>').toString(),
'<h2>Hello world</h2>\n',
'should support sanitation: true'
);

t.equal(
remark().use(html, {
sanitize: null
}).process('## Hello <span>world</span>').toString(),
}).processSync('## Hello <span>world</span>').toString(),
'<h2>Hello <span>world</span></h2>\n',
'should support sanitation: null'
);

t.equal(
remark().use(html, {
sanitize: false
}).process('## Hello <span>world</span>').toString(),
}).processSync('## Hello <span>world</span>').toString(),
'<h2>Hello <span>world</span></h2>\n',
'should support sanitation: false'
);

t.equal(
remark().use(html, {
sanitize: {tagNames: []}
}).process('## Hello <span>world</span>').toString(),
}).processSync('## Hello <span>world</span>').toString(),
'Hello world\n',
'should support sanitation schemas'
);
Expand All @@ -329,7 +329,7 @@ test('Fixtures', function (t) {
file.contents = input;

config = exists(config) ? JSON.parse(read(config, 'utf-8')) : {};
result = process(file, config);
result = processSync(file, config);

t.equal(result, output, 'should work on `' + fixture + '`');
});
Expand All @@ -347,7 +347,7 @@ test('CommonMark', function (t) {
var exception;

file.contents = test.markdown;
result = process(file, CMARK_OPTIONS);
result = processSync(file, CMARK_OPTIONS);

n += 1;

Expand Down Expand Up @@ -389,9 +389,10 @@ test('Integrations', function (t) {
config.sanitize = false;

result = remark()
.data('settings', config)
.use(html, config)
.use(INTEGRATION_MAP[integration], config)
.process(file, config)
.processSync(file)
.toString();

t.equal(result, output, 'should work on `' + integration + '`');
Expand All @@ -400,6 +401,6 @@ test('Integrations', function (t) {
t.end();
});

function process(file, config) {
return remark().use(html, config).process(file, config).toString();
function processSync(file, config) {
return remark().data('settings', config).use(html, config).processSync(file).toString();
}

0 comments on commit 0a27699

Please sign in to comment.