-
I'm trying to add a spoiler functionality to This is the third time I've tried to write such an extension in 9 months, and it's just a genuine nightmare to navigate this and related projects. There is so much special and undocumented behavior hidden away that using the GFM extension as a basis for other things seems useless. The remark-math and rehype-katex packages are possibly closer, as I'm guessing the rehype plugin is what actually handles the new nodes, but those replace the inner text rather than wrap it so I'm at a loss. Furthermore, ideally I'd like to be able to use a custom component (via Any advice or examples would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
See § Extending markdown and § Creating a micromark extension on how to create extensions that change the markdown syntax. You might want to read up on unified as well. My recommendation, though, is probably: don’t do this. |
Beta Was this translation helpful? Give feedback.
-
@novacrazy rather than starting with custom syntax, it may be good to start with directives which are semi-standard and already supported. Some *emphasis*, **importance**, :spoiler[custom **spoiler** *text*] and `code`. As @wooorm notes, writing a custom parser can be complex and time consuming, both to write initially and to maintain over time, whether it's written for remark or another markdown parser. The process of extending remark/micromark is documented at https://github.com/micromark/micromark#extending-markdown |
Beta Was this translation helpful? Give feedback.
-
My solution is to move to https://github.com/Khan/simple-markdown (which actually turns out to be the same lib Discord uses) var spoilerRule = {
match: function(source) {
return /^\|\|([\s\S]+?)\|\|(?!\|)/.exec(source);
},
parse: function(capture, parse, state) {
return { content: parse(capture[1], state) };
},
react: function(node, output, state) {
return <CustomSpoiler key={state.key}>{output(node.content, state)}</CustomSpoiler>
},
}; |
Beta Was this translation helpful? Give feedback.
My solution is to move to https://github.com/Khan/simple-markdown (which actually turns out to be the same lib Discord uses)