Shared `slotsTokenizer` for directive blocks

オープン
#1,301 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@makhnatkin がすでに取り組んでいます。

2026年10月1日 から。

  • #1303 @makhnatkin による — オープン

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
20/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
markdown, typescript
領域
frontend

調査の方向性

Start from the existing cut extension and container directive registration that call builder.configureMd; trace how each parses its body into Markdown-it tokens and where schema expressions are declared. Implement the shared router behind blockContentSlots and slotsTokenizer, then compare both token streams shown in the issue. Done when cut routes paragraphs/images and header routes actions/content, with preview and WYSIWYG specs accepting the same routed stream.

索引モデルが issue の本文から書いたものです。

説明

Edited by AI
"Generated" means mostly the model, "Edited" means mostly the author.

Problem

Different blocks split their Markdown body into several parts. Each part limits which content it accepts and how many items it holds. Implemented in each block's own contentTokenizer, this selection and routing logic is duplicated.

Solution

A shared slotsTokenizer function takes rules for named parts (slots) and returns a tokenizer for the existing contentTokenizer field of container directives.

The block declares its slots and their limits. The shared function parses the body, selects content and emits slot wrappers.

Contract

The field in ContainerDirectiveConfig from @diplodoc/directive:

contentTokenizer?: (
    state: StateBlock,
    content: BlockContent,
    params: ContainerDirectiveParams,
) => void;

The directive handler calls it between the block's opening and closing tokens. The function adds the body tokens to state.tokens.

slotsTokenizer returns a function with this signature:

type SlotMatch = (group: readonly Token[]) => boolean;

type SlotRule = {
    slot: string;
    match: SlotMatch;
    limit?: number;
    className?: string;
};

declare function slotsTokenizer(
    name: string,
    rules: readonly SlotRule[],
): NonNullable<ContainerDirectiveConfig['contentTokenizer']>;

declare function byType(...types: string[]): SlotMatch;
  • name — directive name. The body is parsed with parentType <name>-directive, as by the default tokenizer.
  • slot — unique slot name. The wrapper is a <slot>_open / <slot>_close pair with the div tag.
  • match — condition for content the slot accepts.
  • limit — maximum number of groups in the slot per block instance: a positive integer, unlimited by default.
  • className — optional CSS class of the wrapper.
  • byType — matches the type of the group's first token.

A group is a standalone top-level token, or an opening token with everything up to its matching closing token. A group is moved as a whole.

Processing rules

  1. Groups are processed in source order.
  2. A group goes to the first slot whose match returns true and whose limit is not reached.
  3. A group no slot accepts is dropped. A last slot with match: () => true keeps the remaining content.
  4. Slots are emitted in rule order, and groups keep source order within a slot. Empty slots emit their wrappers too.
  5. Moved tokens get level + 1. A wrapper's map spans its groups.
  6. match receives the whole group with inline.children filled, so it can check nested markup. Markdown-it fills children in the core inline rule after block parsing, so slotsTokenizer fills them itself without duplicates after that rule runs.
  7. Preview and WYSIWYG use the same token stream. Editor slot nodes accept the content their rules accept, including an empty slot.

Usage

registerContainerDirective(md, {
    name: 'block',
    match: () => true,
    container: {tag: 'div', token: 'block'},
    contentTokenizer: slotsTokenizer('block', [
        {slot: 'block-a', match: byType('paragraph_open'), limit: 1},
        {slot: 'block-b', match: byType('paragraph_open', 'bullet_list_open')},
    ]),
});

The first paragraph goes to block-a. Later paragraphs and bullet lists go to block-b. Everything else is dropped.

主要言語
TypeScript
スター
428
フォーク
48
平均マージ
5日 15時間
マージ済み PR(30日)
47

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

gravity-ui/markdown-editor のほかの issue

gravity-ui/markdown-editor の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。