Maintains compatibility with mdbook 0.4.x. Includes comments for what to remove once 0.5.x is the only we care about. Some other changes technically could be changed at that point, but currently serve to enable universal support of 0.4.x and 0.5.x Fixes #1051. Change-Id: Ic5b405038d180bcd357bbd9e5716879e0c26e5f5
32 lines
1001 B
Plaintext
32 lines
1001 B
Plaintext
"\\[\\]\\{#(?<anchor>[^\\}]+?)\\}" as $empty_anchor_regex |
|
|
"\\[(?<text>[^\\]]+?)\\]\\{#(?<anchor>[^\\}]+?)\\}" as $anchor_regex |
|
|
|
|
|
|
def transform_anchors_html:
|
|
. | gsub($empty_anchor_regex; "<a name=\"" + .anchor + "\"></a>")
|
|
| gsub($anchor_regex; "<a href=\"#" + .anchor + "\" id=\"" + .anchor + "\">" + .text + "</a>");
|
|
|
|
|
|
def transform_anchors_strip:
|
|
. | gsub($empty_anchor_regex; "")
|
|
| gsub($anchor_regex; .text);
|
|
|
|
|
|
def map_contents_recursively(transformer):
|
|
. + {
|
|
Chapter: (.Chapter + {
|
|
content: .Chapter.content | transformer,
|
|
sub_items: .Chapter.sub_items | map(map_contents_recursively(transformer)),
|
|
}),
|
|
};
|
|
|
|
|
|
def process_command:
|
|
.[0] as $context |
|
|
.[1] as $body |
|
|
# XXX FUTURE: drop sections once mdBook is at 0.5.0 or above in nixpkgs
|
|
$body | (.items? // .sections) |= map(map_contents_recursively(if $context.renderer == "html" then transform_anchors_html else transform_anchors_strip end))
|
|
;
|
|
|
|
process_command
|