Skip to content

Commit

Permalink
feat: aggregate frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
uetchy committed Jun 24, 2020
1 parent 10cbd94 commit e970720
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 10 deletions.
13 changes: 10 additions & 3 deletions packages/vfm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,37 @@
"debug": "^4.1.1",
"hast-util-is-element": "^1.0.4",
"hastscript": "^5.1.2",
"js-yaml": "^3.14.0",
"mdast-util-to-hast": "^9.1.0",
"mdast-util-to-string": "^1.1.0",
"meow": "^7.0.1",
"rehype-document": "^5.0.0",
"rehype-mathjax": "^2.0.0",
"rehype-raw": "^4.0.2",
"rehype-slug": "^3.0.0",
"rehype-stringify": "^8.0.0",
"remark-breaks": "^1.0.5",
"remark-frontmatter": "^2.0.0",
"remark-math": "^2.0.1",
"remark-parse": "^8.0.2",
"remark-rehype": "^7.0.0",
"to-vfile": "^6.1.0",
"unified": "^9.0.0",
"unist-builder": "^2.0.3",
"unist-util-inspect": "^6.0.0",
"unist-util-select": "^3.0.1",
"unist-util-visit": "^2.0.2"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.0",
"@types/js-yaml": "^3.12.4",
"@types/node": "^14.0.13",
"@types/vfile": "^4.0.0",
"jest": "^26.0.1",
"mdast-util-to-hast": "^9.1.0",
"shx": "^0.3.2",
"ts-jest": "^26.1.0",
"typescript": "^3.9.5",
"unist-util-inspect": "^6.0.0"
"typescript": "^3.9.5"
},
"types": "lib/index.d.ts",
"main": "lib/index.js",
Expand Down
16 changes: 11 additions & 5 deletions packages/vfm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unified from 'unified';
import {VFile} from 'vfile';
import doc from 'rehype-document';
import rehypeStringify from 'rehype-stringify';

Expand All @@ -11,22 +12,27 @@ export interface StringifyMarkdownOptions {
partial?: boolean;
}

export function stringify(
export function process(
markdownString: string,
{stylesheet = undefined, partial = false}: StringifyMarkdownOptions = {},
): string {
): VFile {
const processor = unified().use(markdown).use(html);

if (!partial) {
processor.use(doc, {language: 'ja', css: stylesheet});
}

processor.use(rehypeStringify);

if (debug.enabled) {
const inspect = require('unist-util-inspect');
debug(inspect(processor.parse(markdownString)));
}

return String(processor.processSync(markdownString));
return processor.processSync(markdownString);
}

export function stringify(
markdownString: string,
options: StringifyMarkdownOptions = {},
): string {
return String(process(markdownString, options));
}
32 changes: 32 additions & 0 deletions packages/vfm/src/plugins/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import visit from 'unist-util-visit';
import {select} from 'unist-util-select';
import toString from 'mdast-util-to-string';
import {safeLoad as yaml} from 'js-yaml';
import {Node} from 'unist';
import {VFile} from 'vfile';

interface CFile extends VFile {
data: {title: string};
}

interface CNode extends Node {
value: string;
}

// https://github.com/Symbitic/remark-plugins/blob/master/packages/remark-meta/src/index.js

export function attacher() {
return (tree: Node, file: CFile) => {
const heading = select('heading', tree);
if (heading) {
file.data.title = toString(heading);
}

visit<CNode>(tree, ['yaml'], (node) => {
file.data = {
...file.data,
...yaml(node.value),
};
});
};
}
4 changes: 4 additions & 0 deletions packages/vfm/src/revive-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import unified from 'unified';
import markdown from 'remark-parse';
import math from 'remark-math';
import breaks from 'remark-breaks';
import frontmatter from 'remark-frontmatter';

import {attacher as ruby} from './plugins/ruby';
import {attacher as fencedBlock} from './plugins/fenced-block';
import {attacher as metadata} from './plugins/metadata';

export default [
[markdown, {commonmark: true}],
fencedBlock,
breaks,
ruby,
math,
frontmatter,
metadata,
] as unified.PluggableList<unified.Settings>;
3 changes: 3 additions & 0 deletions packages/vfm/types/remark.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ declare module 'rehype-mathjax';
declare module 'rehype-slug';
declare module 'hastscript';
declare module 'mdast-util-to-hast/lib/all';
declare module 'mdast-util-to-string';
declare module 'hast-util-find-and-replace';
declare module 'hast-util-is-element';

declare module 'to-vfile';

interface Tokenizer {
(
this: TokenizerInstance,
Expand Down
81 changes: 79 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,11 @@
jest-diff "^25.2.1"
pretty-format "^25.2.1"

"@types/js-yaml@^3.12.4":
version "3.12.4"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.12.4.tgz#7d3b534ec35a0585128e2d332db1403ebe057e25"
integrity sha512-fYMgzN+9e28R81weVN49inn/u798ruU91En1ZnGvSZzCRc5jXx9B2EDhlRaWmcO1RIxFHL8AajRXzxDuJu93+A==

"@types/mdast@^3.0.0":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.3.tgz#2d7d671b1cd1ea3deb306ea75036c2a0407d2deb"
Expand Down Expand Up @@ -1520,6 +1525,13 @@
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.9.tgz#fcf01997bbc9f7c09ae5f91383af076d466594e1"
integrity sha512-XDwyIlt/47l2kWLTzw/mtrpLdB+GPSskR2n/PIcPn+VYhVO77rGhRncIR5GPU0KRzXuqkDO+J5qqrG0Y8P6jzQ==

"@types/vfile@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/vfile/-/vfile-4.0.0.tgz#c32d13cbda319bc9f4ab3cacc0263b4ba1dd1ea3"
integrity sha512-eleP0/Cz8uVWxARDLi3Axq2+fDdN4ibAXoC6Pv8p6s7znXaUL7XvhgeIhjCiNMnvlLNP+tmCLd+RuCryGgmtEg==
dependencies:
vfile "*"

"@types/yargs-parser@*":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
Expand Down Expand Up @@ -1958,6 +1970,11 @@ bluebird@^3.5.1, bluebird@^3.5.3, bluebird@^3.5.5:
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==

boolbase@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
integrity sha1-aN/1++YMUes3cl6p4+0xDcwed24=

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -2603,6 +2620,11 @@ cross-spawn@^7.0.0:
shebang-command "^2.0.0"
which "^2.0.1"

css-selector-parser@^1.0.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/css-selector-parser/-/css-selector-parser-1.4.1.tgz#03f9cb8a81c3e5ab2c51684557d5aaf6d2569759"
integrity sha512-HYPSb7y/Z7BNDCOrakL4raGO2zltZkbeXyAd6Tg9obzix6QhzxCotdBl6VT0Dv4vZfJGVz3WL/xaEI9Ly3ul0g==

cssom@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.4.4.tgz#5a66cf93d2d0b661d80bf6a44fb65f5c2e4e0a10"
Expand Down Expand Up @@ -3189,6 +3211,13 @@ fast-url-parser@1.1.3:
dependencies:
punycode "^1.3.2"

fault@^1.0.1:
version "1.0.4"
resolved "https://registry.yarnpkg.com/fault/-/fault-1.0.4.tgz#eafcfc0a6d214fc94601e170df29954a4f842f13"
integrity sha512-CJ0HCB5tL5fYTEA7ToAq5+kTwd++Borf1/bifxd9iT70QcXr4MRrO3Llf8Ifs70q+SJcGHFtnIE/Nw6giCtECA==
dependencies:
format "^0.2.0"

fb-watchman@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.1.tgz#fc84fb39d2709cf3ff6d743706157bb5708a8a85"
Expand Down Expand Up @@ -3296,6 +3325,11 @@ form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"

format@^0.2.0:
version "0.2.2"
resolved "https://registry.yarnpkg.com/format/-/format-0.2.2.tgz#d6170107e9efdc4ed30c9dc39016df942b5cb58b"
integrity sha1-1hcBB+nv3E7TDJ3DkBbflCtctYs=

fragment-cache@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/fragment-cache/-/fragment-cache-0.2.1.tgz#4290fad27f13e89be7f33799c6bc5a0abfff0d19"
Expand Down Expand Up @@ -4739,7 +4773,7 @@ js-tokens@^4.0.0:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

js-yaml@^3.13.1:
js-yaml@^3.13.1, js-yaml@^3.14.0:
version "3.14.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482"
integrity sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==
Expand Down Expand Up @@ -5165,6 +5199,11 @@ mdast-util-to-hast@^9.1.0:
unist-util-position "^3.0.0"
unist-util-visit "^2.0.0"

mdast-util-to-string@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/mdast-util-to-string/-/mdast-util-to-string-1.1.0.tgz#27055500103f51637bd07d01da01eb1967a43527"
integrity sha512-jVU0Nr2B9X3MU4tSK7JP1CMkSvOj7X5l/GboG1tKRw52lLF1x2Ju92Ms9tNetCcbfX3hzlM73zYo2NKkWSfF/A==

mdurl@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e"
Expand Down Expand Up @@ -5590,6 +5629,11 @@ normalize-url@^3.3.0:
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-3.3.0.tgz#b2e1c4dc4f7c6d57743df733a4f5978d18650559"
integrity sha512-U+JJi7duF1o+u2pynbp2zXDW2/PADgC30f0GsHZtRh+HOcXHnw137TrNlyxxRvWW5fjKd3bcLHPxofWuCjaeZg==

not@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/not/-/not-0.1.0.tgz#c9691c1746c55dcfbe54cbd8bd4ff041bc2b519d"
integrity sha1-yWkcF0bFXc++VMvYvU/wQbwrUZ0=

npm-bundled@^1.0.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.1.1.tgz#1edd570865a94cdb1bc8220775e29466c9fb234b"
Expand Down Expand Up @@ -5675,6 +5719,13 @@ npmlog@^4.1.2:
gauge "~2.7.3"
set-blocking "~2.0.0"

nth-check@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/nth-check/-/nth-check-1.0.2.tgz#b2bd295c37e3dd58a3bf0700376663ba4d9cf05c"
integrity sha512-WeBOdju8SnzPN5vTUJYxYUxLeXpCaVP5i5e0LF8fg7WORF2Wd7wFX/pk0tYZk7s8T+J7VLy0Da6J1+wCT0AtHg==
dependencies:
boolbase "~1.0.0"

number-is-nan@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
Expand Down Expand Up @@ -6582,6 +6633,13 @@ remark-breaks@^1.0.5:
resolved "https://registry.yarnpkg.com/remark-breaks/-/remark-breaks-1.0.5.tgz#e9785f8b174f45c05af542fbeb18354b766e1139"
integrity sha512-lr8+TlJI273NjEqL27eUthPYPTCgXEj4NaLbnazS3bQaQL2FySlsbtgo52gE36fE1gWeQgkn1VdmWsoT+uA7FA==

remark-frontmatter@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/remark-frontmatter/-/remark-frontmatter-2.0.0.tgz#c9b8539c27cd23b1672c7e0fcbd5795eeedb4dc1"
integrity sha512-uNOQt4tO14qBFWXenF0MLC4cqo3dv8qiHPGyjCl1rwOT0LomSHpcElbjjVh5CwzElInB38HD8aSRVugKQjeyHA==
dependencies:
fault "^1.0.1"

remark-math@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/remark-math/-/remark-math-2.0.1.tgz#6edc884ba3b40710c2ae31ce93cd88c9959ac8b9"
Expand Down Expand Up @@ -7551,6 +7609,14 @@ to-regex@^3.0.1, to-regex@^3.0.2:
regex-not "^1.0.2"
safe-regex "^1.1.0"

to-vfile@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/to-vfile/-/to-vfile-6.1.0.tgz#5f7a3f65813c2c4e34ee1f7643a5646344627699"
integrity sha512-BxX8EkCxOAZe+D/ToHdDsJcVI4HqQfmw0tCkp31zf3dNP/XWIAjU4CmeuSwsSoOzOTqHPOL0KUzyZqJplkD0Qw==
dependencies:
is-buffer "^2.0.0"
vfile "^4.0.0"

tough-cookie@^2.3.3, tough-cookie@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
Expand Down Expand Up @@ -7814,6 +7880,17 @@ unist-util-remove-position@^2.0.0:
dependencies:
unist-util-visit "^2.0.0"

unist-util-select@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/unist-util-select/-/unist-util-select-3.0.1.tgz#787fc452db9ba77f0ade0e7dc53c3d9d4acc79c7"
integrity sha512-VQpTuqZVJlRbosQdnLdTPIIqwZeU70YZ5aMBOqtFNGeeCdYn6ORZt/9RiaVlbl06ocuf58SVMoFa7a13CSGPMA==
dependencies:
css-selector-parser "^1.0.0"
not "^0.1.0"
nth-check "^1.0.0"
unist-util-is "^4.0.0"
zwitch "^1.0.0"

unist-util-stringify-position@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-2.0.3.tgz#cce3bfa1cdf85ba7375d1d5b17bdc4cada9bd9da"
Expand Down Expand Up @@ -7955,7 +8032,7 @@ vfile-message@^2.0.0:
"@types/unist" "^2.0.0"
unist-util-stringify-position "^2.0.0"

vfile@^4.0.0:
vfile@*, vfile@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/vfile/-/vfile-4.1.1.tgz#282d28cebb609183ac51703001bc18b3e3f17de9"
integrity sha512-lRjkpyDGjVlBA7cDQhQ+gNcvB1BGaTHYuSOcY3S7OhDmBtnzX95FhtZZDecSTDm6aajFymyve6S5DN4ZHGezdQ==
Expand Down

0 comments on commit e970720

Please sign in to comment.