独自形式への変換


@cosense-toolbox/parser/compile は、AST をプレーンテキストや独自形式に変換します。HTML や hast を出力する場合は HTML への変換 を使ってください。

toPlainText


toPlainText(node) は記法を外したテキストを返します。インデント、引用、コードブロック、表の内容はテキスト表現として残します。

TypeScript
import { toPlainText } from "@cosense-toolbox/parser/compile"
toPlainText(parse("タイトル\n[* 太字] と [リンク]"))
// → "タイトル\n太字 と リンク"

全文検索のインデックスや、検索結果の抜粋を作るときに使えます。

createCompiler


createCompiler<Out>({ handlers, fallback }) は、ノード型ごとのハンドラーを組み合わせて変換関数を作ります。出力型 Out は文字列以外でも構いません。

TypeScript
import { createCompiler } from "@cosense-toolbox/parser/compile"
const toSimpleText = createCompiler<string>({
handlers: {
internalLink: (node) => node.target,
decoration: (_node, context) => context.children(_node).join(""),
text: (node) => node.value,
},
fallback: (node, context) => context.children(node).join(""),
})

ハンドラーを定義していないノードは fallback に渡ります。ctx.children(node) から子ノードの変換結果を取得できます。

ハンドラーの型は AST のノード型から導出されます。独自ノードを追加すると、その型もコンパイラーに反映されます。