Skip to content

Refactor processMarkdown and DocumentProcessorFactory to support arbitrary unified processors #541

@u1f992

Description

@u1f992

cc: @Mura-Mi

#526 でunifiedプロセッサを注入できるようになりましたが、processMarkdownではVFMのreadMetadataを実行しており、指定したプロセッサとは関係なく一度remark-parseでパースしています。そのため、実際にはremark-parseでエラーが発生しないMarkdown記法しか組み込めません。Markdownは寛容なので大抵は不正確にパースされた上でmetadataにでたらめな値が返るだけですが、正しく処理されない拡張Markdownも考えることはできます。

デフォルトのdocumentProcessorから返されるプロセッサを、VFMを直接使用する形からreadMetadataを行った後パースする実装に変更することで、processMarkdownからreadMetadataを取り除き、任意のunifiedプロセッサ・任意の文書形式を使用できるようになると考えています。DocumentProcessorFactoryの引数からもmetadataは取り除かれます。

たたき台案(参考:https://github.com/orgs/unifiedjs/discussions/113#discussioncomment-234473

const defaultVFMFactory = (options) =>
    unified().use(function () {
        this._vfm = VFM(options);
        this.Parser = (text, file) => {
            this._vfm = VFM(options, readMetadata(text));
            return this._vfm.runSync(this._vfm.parse(file), file);
        };
        this.Compiler = (node, file) => {
            return this._vfm.stringify(node, file);
        };
    });

DocumentProcessorFactoryの引数からmetadataを取り除くデメリットとして、metadataを変更してVFMプロセッサで使用するカスタムプロセッサは作成しづらくなります。しかし、メタデータの多くはhastに変換後もdocumentProcessor: (options) => defaultVFMFactory(options).use(/* metadata操作プラグイン */)のような形で操作できるので問題は少ないと考えます。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions