commonmark / commonmark/commonmark-java

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

Ouverte
#428 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

enhancement
Langage dominant
Java
Étoiles
2.7k
Forks
336
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par InlineParserImpl et suivez comment les delimiter runs deviennent des DelimiterData et comment processDelimiters appelle DelimitedProcessor.process(). Examinez le chemin de inlineParserFactory et déterminez où un comportement personnalisé de canOpen/canClose ou de flanking pourrait être exposé sans copier InlineParserImpl. C’est terminé lorsqu’un consommateur peut définir des règles de délimiteurs personnalisées et que la syntaxe « loose » demandée pour le gras et l’italique est analysée en conséquence.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
java
Domaine
tooling
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Active
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.