-
Notifications
You must be signed in to change notification settings - Fork 50
Modify mdParser to offer config extension like nuxtjs and expose an hook for customizing of created parser #119
Conversation
mdParser receives rules that are specified within the config and passes them to the markdown parser
|
You are right - currently this lacks flexibility. But I personally don't like the idea to include parts of markdown-it's API in nuxtents configuration. Next to renderer rules markdown-it also supports inline token rules, block token rules and other things. Currently I'd suggest one of the following approaches. Make it optional to pass the whole renderer to nuxtentFrom the configuration point of view this might look like this var md = require('markdown-it-emoji');
var emoji = require('markdown-it-emoji');
var twemoji = require('twemoji')
const myMD = md()
myMD.use(emoji)
myMD.renderer.rules['emoji'] = function(token, idx) {
return twemoji.parse(token[idx].content);
}
// do whatever with the parser
module.exports = {
// ...
parsers: {
md: myMD
}
}For this solution the plugins that nuxtent currently applies behind the scenes need to be applied on the given instance. This might lead to problems when someone already configured them in front. Provide a hook with the constructedAnother option would be to support a hook method that receives the parser constructed by nuxtent. var md = require('markdown-it-emoji');
var emoji = require('markdown-it-emoji');
var twemoji = require('twemoji')
module.exports = {
// ...
parsers: {
md: {
use: [
emoji
],
postParserConstruct: function (parser) {
parser.renderer.rules[emoji] = function(token, idx) {
return twemoji.parse(token[idx].content);
}
}
}
}
}
This one should be quite easy to implement. |
|
@dahrens, good point. Thank you for your suggestions. Regarding your description, I don't like the downsides of approach number one. Looks too much like a workaround. I think I will make a proposal for your second suggestion. In every software where I want to change or add behaviour, I really like the possibility of using hooks. This can offer more flexibility in terms of different renderer and configuration options and decouples the nuxtent configuration from all the other possible configurations. You are right, the implementation for integrating that hook should be quite simple. I will do a first proposal next week. |
|
Another option could be to configure it similar to nuxtent modules: with rules: [use: Object, rules: Function] |
|
Or to get rid of |
|
@alidcastano, in the first step I introduced the discussed The corresponding configuration for the introduced extension looks like already mentioned by @dahrens. |
|
If we throw out The configuration should than look like that IMO: module.exports = {
// ...
parsers: {
md: parser => {
// do whatever you want to do with the parser
}
}
}I like that as it gives the user all possibilities to do whatever with markdown-it. @alidcastano what is the problem with appveyor raising errors here? Looks like the fail is not related to this PR, but to the overall configuration. |
|
@dahrens and @alidcastano, I thought about your ideas of stripping the code in |
|
@dahrens, I read your comment a second time and thought about it. Did you imagine a configuration like this? |
|
There is currently at least one features in nuxtent that relies on markdown-it to be configured as it is. This is how markdown-it-anchors gets configured based on the anchor level provided in the nuxtent content configuration. I'd suggest to keep the initial configuration of the parser where it is - but instead of merging this with incoming use parameter I would just provide the @alidcastano are there any other parts of the code, that rely on the config passed to markdown-it initially or can we move this configuration as is into the nuxtent configuration? I'll add commets to the changeset to make this more clear. |
| highlight | ||
| }) | ||
|
|
||
| const plugins = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part should stay here.
| { | ||
| level: [anchorsLevel] | ||
| } | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we must not merge this with incoming use.
lib/util/parsers.js
Outdated
| const parser = markdownit(config) | ||
|
|
||
| if (postConstructionHook !== undefined ){ | ||
| postConstructionHook(parser) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because every other plugin might get loaded in the hook afterwards.
| import yamlit from 'js-yaml' | ||
| import markdownit from 'markdown-it' | ||
| import markdownAnchors from 'markdown-it-anchor' | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea to pass the configuration from the outside, but I'm currently not aware of all possible side effects.
|
I think it should work similar to how Nuxt uses webpack but allows users to extend it. Nuxtent can use markdown it (provide default configuration along with anchor plugin - yes @dahrens that is the only part that uses it directly). |
|
Yeah the build is failing due to previous push. Was waiting for @medfreeman module refactor to check it but he's been busy with work so I'll see if I can find time this week to get this all working |
|
Ok, I reintroduced the default config object, I named it |
|
During studying the nuxt.js configuration docs I realized, that I could change the mechanism of extending the |
|
Hi @arrkiin thanks for devoting time to this. The last API you proposed seems too complicated. Since Nuxtent already uses markdown-it, we can make things simpler for users. What do you think of this: And can be used as such: The first part is similar to what you had, except API copies how Nuxt does extends Webpack so that usage is familiar with users. The second part, rather than having users initialize plugins themselves, we do it for them, and pass the parser as an extra parameter for advanced customization. While the usage is similar, now customizing markdown-it seems less daunting :) |
|
I think I would split the parser-extension-callback off the plugins array. What about the variant below? There we have |
|
Delicious! ;) |
To support the twemoji output for markdown-it-emoji, I had to figure out how to pass the described mapping function to the rules object of markdown-it.
Example from markdown-it-emoji:
So I extended the mdParser so that it receives rules that are specified within the config and passes them to the markdown parser. An example for a config file shows like this: