Closed CPColin closed 6 years ago
Looks like we need to extend the parser, no matter what, because double brackets, on their own, don't parse into a link node.
Current strategy, inspired by rjeschke/txtmark
, which is what the Ceylon IDE code currently uses:
\[\[.*?\]\]
transformSpecialLinks(Node root, Node?(String) transformation)
. That transformation
function could maybe return String?
and [String, String]?
also. (Tinker with this and see what works best.)"[[" + render(node.children) + "]]"
. The assumption is that any "special link" nodes still in the AST were unable to be transformed.Here's an example of how one would transform special links:
import ceylon.markdown.parser {
Node,
NodeType,
ParseOptions,
Parser,
transformSpecialLinks
}
import ceylon.markdown.renderer {
RawHtmlRenderer
}
Node createLinkNode(String destination, String content) {
value node = Node(NodeType.link);
node.destination = destination;
value text = Node(NodeType.text);
text.literal = content;
node.appendChild(text);
return node;
}
shared void run() {
value input
= "## Testing special links
Broken link: [[broken]], working link: [[link]].";
value parseOptions = ParseOptions {
specialLinks = true;
};
value parser = Parser(parseOptions);
value root = parser.parse(input);
function transform(String content)
=> content != "broken" then createLinkNode("doc:``content``", content.uppercased);
transformSpecialLinks(root, transform);
value renderer = RawHtmlRenderer();
print(renderer.render(root));
}
The module will be useful for displaying Ceylon documentation only if the
Parser
can handle wiki-style links that are delimited with double brackets. Rather than add such support to the code that was ported fromcommonmark.js
, we should see if we can use thecustom_inline
orcustom_block
node types here. If we can't, we should see if we can extend the parser and renderers in some other way.Another possibility is to implement this change via the renderer only. That is, if the parser produces a link node whose destination begins and ends with brackets, the renderer could (attempt to) treat it as a wiki link. This strategy would make sense for things like the IDE and the documentation tool, because they'd need to make their own metadata lookups and transformations to the links.