Skip to content

Commit

Permalink
feat: add options (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverBehave committed May 4, 2020
1 parent 5c448ee commit 532ab83
Show file tree
Hide file tree
Showing 39 changed files with 668 additions and 74 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ remark().use(pangu).process(doc, (err, file) => {
// => 中文 abc 中文
```

### Options

可以通过指定部分选项为 `false` 以跳过部分节点的排版处理

e.g. 不处理 `inline code` 的内容

```js
remark().use(pangu, {
inlineCode: false
})
```

#### Default Options

```js
{
text: true,
inlineCode: true,
link: true,
image: true,
imageReference: true,
definition: true,
}
```

## LICENSE

[MIT](./LICENSE)
10 changes: 10 additions & 0 deletions defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

module.exports = {
text: true,
inlineCode: true,
link: true,
image: true,
imageReference: true,
definition: true,
};
38 changes: 22 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'
"use strict";

const visit = require('unist-util-visit')
const is = require('unist-util-is')
const pangu = require('pangu')
const visit = require("unist-util-visit");
const is = require("unist-util-is");
const pangu = require("pangu");
const setOptions = require("./set-options");

// List of Markdown AST: <https://github.com/syntax-tree/mdast>
// AST Explorer: <https://astexplorer.net/#/gist/7a794a8fc43b2e75e27024c85fb77aad/0934495eb735dffdf739dc7943f7848940070f8e>
Expand Down Expand Up @@ -36,26 +37,31 @@ const pangu = require('pangu')
// 3. ...

function format(value) {
if (!value) return value
return pangu.spacing(value)
if (!value) return value;
return pangu.spacing(value);
}

function visitor(node) {
if (is(node, 'text') || is(node, 'inlineCode')) {
node.value = format(node.value)
if (is(node, "text") || is(node, "inlineCode")) {
node.value = format(node.value);
}

if (is(node, 'link') || is(node, 'image') || is(node, 'definition')) {
node.title = format(node.title)
if (is(node, "link") || is(node, "image") || is(node, "definition")) {
node.title = format(node.title);
}

if (is(node, 'image') || is(node, 'imageReference')) {
node.alt = format(node.alt)
if (is(node, "image") || is(node, "imageReference")) {
node.alt = format(node.alt);
}
}

module.exports = function attacher() {
module.exports = function attacher(options) {
const settings = setOptions(options || {});
const subset = Object.keys(settings).filter((e) => settings[e]);

return function transformer(tree, file) {
visit(tree, visitor)
}
}
visit(tree, (node) => {
if (is(node, subset)) visitor(node);
});
};
};
12 changes: 12 additions & 0 deletions set-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

const defaults = require("./defaults");

module.exports = (options) => {
let mixed = Object.assign({}, defaults);
for (const key in options) {
if (typeof options[key] === "boolean") mixed[key] = options[key];
}

return mixed;
};
78 changes: 39 additions & 39 deletions tests-config/setup.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
'use strict'
"use strict";

const fs = require('fs')
const { extname } = require('path')
const unified = require('unified')
const parse = require('remark-parse')
const stringify = require('remark-stringify')
const frontmatter = require('remark-frontmatter')
const footnotes = require('remark-footnotes')
const pangu = require('../index')
const fs = require("fs");
const { extname } = require("path");
const unified = require("unified");
const parse = require("remark-parse");
const stringify = require("remark-stringify");
const frontmatter = require("remark-frontmatter");
const footnotes = require("remark-footnotes");
const pangu = require("../index");

const processor = unified()
.use(parse, { footnotes: true })
.use(stringify)
.use(frontmatter)
.use(footnotes, {inlineNotes: true})
.use(pangu)
.freeze()

function formatAsync(source) {
return processor.process(source)
}
const createProcessor = function (options) {
return unified()
.use(parse, { footnotes: true })
.use(stringify)
.use(frontmatter)
.use(footnotes, { inlineNotes: true })
.use(pangu, options)
.freeze();
};

function raw(string) {
if (typeof string !== 'string') {
throw new Error('Raw snapshots have to be strings.')
if (typeof string !== "string") {
throw new Error("Raw snapshots have to be strings.");
}
return { [Symbol.for('raw')]: string }
return { [Symbol.for("raw")]: string };
}

function runSpec(dirname) {
fs.readdirSync(dirname).forEach(filename => {
const path = dirname + '/' + filename
function runSpec(dirname, options) {
fs.readdirSync(dirname).forEach((filename) => {
const path = dirname + "/" + filename;
if (
extname(filename) !== '.snap' &&
extname(filename) !== ".snap" &&
fs.lstatSync(path).isFile() &&
filename !== 'run.spec.js'
filename !== "run.spec.js"
) {
const source = fs.readFileSync(path)
test(`${filename}`, () => {
return formatAsync(source).then(vfile => {
const output = String(vfile)
return expect(
raw(source + '~'.repeat(80) + '\n' + output),
).toMatchSnapshot(filename)
})
})
const source = fs.readFileSync(path);
test(`${filename} with options: ${JSON.stringify(options)}`, () => {
return createProcessor(options)
.process(source)
.then((vfile) => {
const output = String(vfile);
return expect(
raw(source + "~".repeat(80) + "\n" + output)
).toMatchSnapshot(filename);
});
});
}
})
});
}

global.runSpec = runSpec
global.runSpec = runSpec;
29 changes: 28 additions & 1 deletion tests/blockquote/__snapshots__/run.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`blockquote.md: blockquote.md 1`] = `
exports[`blockquote.md with options: {"text":false}: blockquote.md 1`] = `
> abc123
> 我a我1
> _abc123_
> _我a我1_
> **abc123**
> **我a我1**
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> abc123
> 我a我1
> _abc123_
> _我a我1_
> **abc123**
> **我a我1**
`;

exports[`blockquote.md with options: undefined: blockquote.md 1`] = `
> abc123
> 我a我1
Expand Down
3 changes: 3 additions & 0 deletions tests/blockquote/run.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
runSpec(__dirname)
runSpec(__dirname, {
text: false
})
2 changes: 1 addition & 1 deletion tests/code/__snapshots__/run.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`code.md: code.md 1`] = `
exports[`code.md with options: undefined: code.md 1`] = `
\`\`\`javascript
const a = 123
const b = 'abc123'
Expand Down
2 changes: 1 addition & 1 deletion tests/code/run.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
runSpec(__dirname)
runSpec(__dirname)
17 changes: 16 additions & 1 deletion tests/definition/__snapshots__/run.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`definition.md: definition.md 1`] = `
exports[`definition.md with options: {"definition":false}: definition.md 1`] = `
[链接a引用1][link]
![图片b引用2][link]
[link]: http://example.com/链接a链接1 "标题a标题1"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[链接 a 引用 1][link]
![图片 b 引用 2][link]
[link]: http://example.com/链接a链接1 "标题a标题1"
`;

exports[`definition.md with options: undefined: definition.md 1`] = `
[链接a引用1][link]
![图片b引用2][link]
Expand Down
3 changes: 3 additions & 0 deletions tests/definition/run.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
runSpec(__dirname)
runSpec(__dirname, {
definition: false
})
13 changes: 12 additions & 1 deletion tests/delete/__snapshots__/run.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`delete.md: delete.md 1`] = `
exports[`delete.md with options: {"text":false}: delete.md 1`] = `
~~abc123~~
~~我a我1~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~abc123~~
~~我a我1~~
`;

exports[`delete.md with options: undefined: delete.md 1`] = `
~~abc123~~
~~我a我1~~
Expand Down
3 changes: 3 additions & 0 deletions tests/delete/run.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
runSpec(__dirname)
runSpec(__dirname, {
text: false
})
13 changes: 12 additions & 1 deletion tests/emphasis/__snapshots__/run.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`emphasis.md: emphasis.md 1`] = `
exports[`emphasis.md with options: {"text":false}: emphasis.md 1`] = `
_abc123_
_强调a强调1_
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_abc123_
_强调a强调1_
`;

exports[`emphasis.md with options: undefined: emphasis.md 1`] = `
_abc123_
_强调a强调1_
Expand Down
5 changes: 4 additions & 1 deletion tests/emphasis/run.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
runSpec(__dirname)
runSpec(__dirname)
runSpec(__dirname, {
text: false
})
21 changes: 20 additions & 1 deletion tests/footnote/__snapshots__/run.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`footnote.md: footnote.md 1`] = `
exports[`footnote.md with options: {"text":false}: footnote.md 1`] = `
lol[^footnote]
我的a天[^脚注1]
[^footnote]: abc123 我1我a
[^脚注1]: acb123 我1我a
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lol[^footnote]
我的a天[^脚注1]
[^footnote]: abc123 我1我a
[^脚注1]: acb123 我1我a
`;

exports[`footnote.md with options: undefined: footnote.md 1`] = `
lol[^footnote]
我的a天[^脚注1]
Expand Down
3 changes: 3 additions & 0 deletions tests/footnote/run.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
runSpec(__dirname)
runSpec(__dirname, {
text: false
})
Loading

0 comments on commit 532ab83

Please sign in to comment.