Created
March 25, 2020 17:44
-
-
Save mizchi/ae6354efcc73dd71c2bc463635338df4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unified from "unified"; | |
// @ts-ignore | |
import visit from "unist-util-visit"; | |
import u from "unist-builder"; | |
// var tree = u("root", [ | |
// u("subtree", { id: 1 }), | |
// u("subtree", { id: 2 }, [ | |
// u("node", [u("leaf", "leaf 1"), u("leaf", "leaf 2")]), | |
// u("leaf", { id: 3 }, "leaf 3"), | |
// u("void", { id: 4 }) | |
// ]) | |
// ]); | |
const parser: unified.Plugin = function() { | |
// @ts-ignore | |
this.Parser = class { | |
constructor(private input: any) {} | |
parse() { | |
console.log("[parser]", this.input); | |
return { type: "root", data: this.input }; | |
} | |
}; | |
}; | |
const plugin: unified.Plugin = function(option: any) { | |
return function(tree: any, file: any) { | |
tree.passed = true; | |
console.log("[process-plugin]", tree); | |
}; | |
}; | |
const compiler: unified.Plugin = function(config: any) { | |
// @ts-ignore | |
this.Compiler = class { | |
constructor(private tree: any) {} | |
compile(tree: any) { | |
console.log("[compile]", this.tree); | |
return this.tree; | |
} | |
}; | |
// @ts-ignore | |
// this.Compiler = Compiler; | |
}; | |
const fn = unified() | |
.use(parser) | |
.use(plugin as any) | |
.use(compiler as any); | |
// .use(compilerPlugin); | |
// const ret = fn.processSync("raw-text"); | |
let ret; | |
// ret = fn.parse("hello"); | |
// const file = vfile(); | |
// file.contents = "hello"; | |
ret = fn.runSync({ type: "root" }); | |
ret = fn.stringify(ret); | |
// @ts-ignore | |
console.log(ret); //file); | |
/* まとめ | |
## Overview | |
- プラグイン間で this は共有される | |
- fn().use()...process() は parse, run, stringify を順番に行う | |
## Parse ステップ | |
- いずれかの plugin の中で、必ず最低一つの this.Parser を定義することを要求される | |
- Parser は引数として文字列を要求し、 返り値は usist の {type: "root", data: {...}} にする必要がある | |
- unified()...parse() すると、this.Parse() が実行される | |
## Run step | |
- unified()...run() すると、すべてのプラグインの関数を実行する: return (tree, file) => {...} | |
- ここは非同期でも良い | |
## Compile step | |
- いずれかの plugin の中で、必ず最低一つの this.Compiler を定義することが要求される | |
- stringify の結果は必ず文字列を要求する(remark-react は結果として react 入ってるけど、 どうなってんの?) | |
// 最初の plugin は必ず parser を要求される | |
`processSync` は parse => run => stringify | |
*/ | |
// @ts-ignore | |
// console.dir("result", ret.contents, { depth: null }); | |
// console.dir(tree, { depth: null }); | |
// const fn unified() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment