commonmark-java icon indicating copy to clipboard operation
commonmark-java copied to clipboard

Allow setting a custom `LinkReferenceDefinitions` in `Parser.Builder`

Open pron opened this issue 1 year ago • 1 comments

An application employing Markdown may want to resolve "naked links" ([foo]) -- i.e. links with no inline destination -- based on contextual knowledge rather than definitions in the text. A custom LinkReferenceDefinition would allow for that.

pron avatar Feb 15 '23 16:02 pron

Hmm that's an interesting idea. So you would set all the known LinkReferenceDefinition in the parser builder beforehand? I wonder if it would be even more useful to provide a callback instead, then you'd have more flexibility and e.g. be able to only look up a definition for which there were actual references in the document.

Something like:

ReferenceLinkResolver resolver = (label) -> {
    if (label.equals("foo")) {
        return new LinkReferenceDefinition("Foo", "https://example.org", "Title");
    } else {
        return null;
    }
};
var parser = Parser.builder().referenceLinkResolver(resolver).build();

robinst avatar Feb 11 '24 11:02 robinst