pegdown icon indicating copy to clipboard operation
pegdown copied to clipboard

GFM like headers processing

Open elennaro opened this issue 10 years ago • 4 comments

In many situations we need to have #hashtags support. There is Two approaches:

  1. GFM like headers - must put a Spasechar() after a header.
  2. Disable H1, some projects do not need to let users to Put H1 to their texts. It wold be good to see GFM like headers switch option in pegdown. However if you are not planning to add it, It'll be great if you explain me how to set priority to my custom rules over those one in Parser?

elennaro avatar Oct 13 '14 16:10 elennaro

I second this feature.

steveluo avatar May 21 '15 08:05 steveluo

Your first option is the best solution, it is a very small change in two files: Parser.java and Extensions.java to get a new extension HEADERSPACE to require a space after Atx header prefix. The change passes all regression tests, so it is safe to use.

The change would not include this new extension in Extensions.ALL because that could break existing tests but it can also be done if someone has time on their hands. :)

Parser.java, replace one line in AtxHeading rule:

    public Rule AtxHeading() {
        return Sequence(
                AtxStart(),

replace this:

                Optional(Sp()),

with:

                (ext(HEADERSPACE) ? Spacechar() : EMPTY), Sp(),
                OneOrMore(AtxInline(), addAsChild()),
                wrapInAnchor(),
                Optional(Sp(), ZeroOrMore('#'), Sp()),
                Newline()
        );
    }

and in Extensions.java, change:

    /**
     * All available extensions excluding the SUPPRESS_... options and HEADERSPACE.
     */
    static final int ALL = 0x0000FFFF;

to:

    /**
     * Requires a space char after Atx # header prefixes, so that #dasdsdaf is not a header.
     */
    static final int HEADERSPACE = 0x800;

    /**
     * All available extensions excluding the SUPPRESS_... options and HEADERSPACE.
     */
    static final int ALL = (0x0000FFFF & ~HEADERSPACE);

vsch avatar Aug 17 '15 02:08 vsch

Closed by #181.

sirthias avatar Aug 22 '15 21:08 sirthias

Now you can enable extension ATXHEADERSPACE to have the headers require a space.

vsch avatar Aug 23 '15 01:08 vsch