Help with markdown-it link rendering

I’m working on a markdown-it based function to extract the text content from HTML, as part of a PR. It seems to work fine for all cases but one. When I try to parse text inside square braces (such as [test]), markdown-it throws an error. My understanding is that it tries to parse it as a link, and fails since there is no link target. However, this does not show any issues in Joplin itself. So, does anyone know what I’m missing here? Is it a setting, a plugin that needs to be enabled, or something else?

The most important info is missing from your post, which is what error is being thrown.

Ah sorry! Here is the error message:

    if (typeof state.env.references === 'undefined') { return false; }

TypeError: Cannot read property 'references' of undefined
    at (/home/anirudh/Desktop/mdit_test/node_modules/markdown-it/lib/rules_inline/link.js:101:26)
    at ParserInline.tokenize (/home/anirudh/Desktop/mdit_test/node_modules/markdown-it/lib/parser_inline.js:135:22)
    at ParserInline.parse (/home/anirudh/Desktop/mdit_test/node_modules/markdown-it/lib/parser_inline.js:163:8)
    at Array.inline (/home/anirudh/Desktop/mdit_test/node_modules/markdown-it/lib/rules_core/inline.js:10:23)
    at Core.process (/home/anirudh/Desktop/mdit_test/node_modules/markdown-it/lib/parser_core.js:51:13)
    at MarkdownIt.parseInline (/home/anirudh/Desktop/mdit_test/node_modules/markdown-it/lib/index.js:560:13)
    at Object.<anonymous> (/home/anirudh/Desktop/mdit_test/index.js:69:26)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)

Obviously, what’s equally important is to post your code…

From the error I’m guessing you’re using the parse function but you’re not passing the env parameter, but again just a guess. And if you’re using parse, you probably shouldn’t.

Oops! I’ll keep that in mind next time I ask for help.

As for the code, it was just a toy code, really. I was just declaring a string and trying to parse it as:

const MarkdownIt = require('markdown-it')();

const test = '[testlink]';


This resulted in the above error. Looks like I misread the API documentation, and thought that an empty object was the default env parameter. Passing an empty object as the second parameter makes it work as expected.

My eventual application would be to use parse, as I’m trying to extract the text by converting the markdown to a stream of tokens and extracting only the content of the text tokens from that. Is that the wrong way to go about it? If so, how should I approach this?

I’ve done that before and that was a mistake as their parse method really shouldn’t be used directly. Instead use the regular render() method along with a custom plugin you’d write. From the plugin you can access all the tokens, like from parse but in a cleaner way.

Thanks for the advice! I’ll look into doing it this way.

Another related question. For this to be successful, I would need the markdown-it instance with all the plugins set up as the current instance of Joplin, if I’m not mistaken. Currently there does not seem to be a way to access the initialized renderer instance, as plugins are set and rendering is done in the same function. Would it make sense to split this into two functions, to be able to access the renderer instance? Or is there another way to access it?

If you only want to extract the plain text, I don’t think you do?

On second thoughts, I think you are right. It would have been an issue if we were using plugins to create new token types, but since Joplin adheres to CommonMark spec, which should be completely supported by default, this shouldn’t be an issue. I’ll work on the renderer and update my progress on the issue. Thanks!

1 Like