pegdown
pegdown copied to clipboard
GFM like headers processing
In many situations we need to have #hashtags support. There is Two approaches:
- GFM like headers - must put a Spasechar() after a header.
- 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?
I second this feature.
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);
Closed by #181.
Now you can enable extension ATXHEADERSPACE to have the headers require a space.