sirthias / pegdown

A pure-Java Markdown processor based on a parboiled PEG parser supporting a number of extensions
http://pegdown.org
Apache License 2.0
1.29k stars 218 forks source link

GFM like headers processing #144

Open elennaro opened 9 years ago

elennaro commented 9 years ago

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?
steveluo commented 9 years ago

I second this feature.

vsch commented 9 years ago

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);
sirthias commented 9 years ago

Closed by #181.

vsch commented 9 years ago

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