CPColin / ceylon.markdown

A port of the commonmark.js Markdown parser and renderer to Ceylon
Apache License 2.0
5 stars 0 forks source link

Support extending parser to handle wiki-style links #10

Closed CPColin closed 6 years ago

CPColin commented 6 years ago

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 from commonmark.js, we should see if we can use the custom_inline or custom_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.

CPColin commented 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.

CPColin commented 6 years ago

Current strategy, inspired by rjeschke/txtmark, which is what the Ceylon IDE code currently uses:

CPColin commented 6 years ago

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));
}