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

Wiki Style Links with alternate text [[page name|page link text]] is not properly rendered. #182

Closed vsch closed 9 years ago

vsch commented 9 years ago

Wiki style links can provide separate text for the page name and the link text in the form [[page name|page link text]]. Which should generate:

<a href="./page-name.html">page link text</a>

instead of:

<a href="./page-name%7Cpage-link-text.html">page name|page link text</a>

The change required is in LinkRenderer.java:

    public Rendering render(WikiLinkNode node) {
        try {
            // vsch: handle WikiLinks alternative format [[page|text]]
            String text = node.getText();
            String url = text;
            int pos;
            if ((pos = text.indexOf("|")) >= 0) {
                url = text.substring(0, pos);
                text = text.substring(pos+1);
            }

            url = "./" + URLEncoder.encode(url.replace(' ', '-'), "UTF-8") + ".html";
            return new Rendering(url, text);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException();
        }
    }

I have the changes and the modified tests. I will accumulate a few fixes before posting a PR.