commonmark / commonmark/commonmark-java

Option to define custom flanking/canOpen/canClose rules for delimiters

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

まだ誰も着手していません。

enhancement
主要言語
Java
スター
2.7k
フォーク
336
PR マージ指標
30日以内にマージされた PR はありません

説明

Is your feature request related to a problem? Please describe.
The markdown spec that I'm trying to parse has loose rules when it comes to bold and italic delimiters, i.e. *italic * is vaild syntax. These seem to be the rules:

  • _ - same behaviour as default
  • __ (as opener) - must have a space/punctuation on the left, but what's on the right doesn't matter
  • __ (as closer), *, ** - no rules (can be surrounded by anything, can have spaces on either side, and still work)

However, Commonmark-java follows the default markdown spec which is strict and has certain flanking rules. Currently these rules seem to be hardcoded in InlineParserImpl:

        boolean beforeIsPunctuation = before == Scanner.END || Characters.isPunctuationCodePoint(before);
        boolean beforeIsWhitespace = before == Scanner.END || Characters.isWhitespaceCodePoint(before);
        boolean afterIsPunctuation = after == Scanner.END || Characters.isPunctuationCodePoint(after);
        boolean afterIsWhitespace = after == Scanner.END || Characters.isWhitespaceCodePoint(after);

        boolean leftFlanking = !afterIsWhitespace &&
                (!afterIsPunctuation || beforeIsWhitespace || beforeIsPunctuation);
        boolean rightFlanking = !beforeIsWhitespace &&
                (!beforeIsPunctuation || afterIsWhitespace || afterIsPunctuation);
        boolean canOpen;
        boolean canClose;
        if (delimiterChar == '_') {
            canOpen = leftFlanking && (!rightFlanking || beforeIsPunctuation);
            canClose = rightFlanking && (!leftFlanking || afterIsPunctuation);
        } else {
            canOpen = leftFlanking && delimiterChar == delimiterProcessor.getOpeningCharacter();
            canClose = rightFlanking && delimiterChar == delimiterProcessor.getClosingCharacter();
        }

        return new DelimiterData(delimiters, canOpen, canClose);

As far as I understand this cannot be solved with custom DelimitedProcessors, for DelimitedProcessor.process() to be called, canOpen() (which is calculated in the code snippet above) must be true in processDelimiters(Delimiter):

...
if (opener.canOpen() && opener.delimiterChar == openingDelimiterChar) {
    potentialOpenerFound = true;
    usedDelims = delimiterProcessor.process(opener, closer);
    ...

So in other words I couldn't find an easy way to change this behaviour.

Describe the solution you'd like
I would like for it to be possible to somehow define custom flanking rules for certain delimiter runs. Maybe the part in InlineParserImpl responsible for calculating canOpen/canClose and constructing DelimiterData could be lifted into an interface of some kind, or a public method in InlineParserImpl so that it can be inherited from and the method overriden.

Describe alternatives you've considered
Copying InlineParserImpl over to my project and changing the relevant lines, then using it via inlineParserFactory when building a Parser

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

調査の方向性

InlineParserImpl から始め、delimiter runs がどのように DelimiterData になり、processDelimiters がどのように DelimitedProcessor.process() を呼び出すかを追跡します。inlineParserFactory の経路を確認し、InlineParserImpl をコピーせずにカスタムの canOpen/canClose または flanking の動作を公開できる箇所を特定します。完了条件は、consumer がカスタムの delimiter ルールを定義でき、要求された loose な太字および斜体の構文がそれに応じてパースされることです。

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

評価

技術スタック
java
領域
tooling
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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