remarkable icon indicating copy to clipboard operation
remarkable copied to clipboard

How to create a plugin?

Open frankandrobot opened this issue 8 years ago • 5 comments

Please don't say https://github.com/rlidwka/remarkable-regexp

I don't want to use a regexp. I'm trying to write a plugin that converts camecase words to links. If I use regexps I might as well #split(' ') the document string and process tokens one at time outside of Remarkable.

frankandrobot avatar Apr 26 '16 14:04 frankandrobot

I'm wondering about the plugin too.. I need a strip markdown plugin.

jcalfee avatar May 05 '16 17:05 jcalfee

I worked up an example by looking at another plugin... A better example would show adding a custom rule..

// remarkableStripper.js
/** Removes all markdown leaving just plain text */
export default md => {
    md.renderer.render = (tokens, options, env) => {
        let str = ''
        for (let i = 0; i < tokens.length; i++) {
            if (tokens[i].type === 'inline') {
                str += md.renderer.render(tokens[i].children, options, env);
            } else {
                // console.log('content', tokens[i])
                const content = tokens[i].content
                str += (content || '')
            }
        }
        return str
    }
}
// other file
import remarkableStripper from 'app/remarkableStripper'

const remarkable = new Remarkable()
remarkable.use(remarkableStripper) // removes all markdown

jcalfee avatar May 05 '16 18:05 jcalfee

@jcalfee thanks for the example!

A better example would show adding a custom rule..

Take a look at pretty-remarkable. I've created custom rules for most of the renderers so that it will render formatted markdown instead of HTML.

jonschlinkert avatar May 05 '16 18:05 jonschlinkert

I had to create a plugin too (I wrote about it here), and your examples helped me a lot, thanks ! But still I don't really get the difference between state.line and the startLine in the context of a block rule function. Does someone have an explanation ?

ybycode avatar May 27 '16 11:05 ybycode

@ybycode As far as I understand, state.line is pretty much internal data and should not be used, modified or relied upon. In particular, you are not guaranteed it will be updated in case of nested calls to the block parser. (If you want more info, you might check my doc PR regarding plugins, link above my comment. It's not quite finished yet, but getting feedback on it WILL help! Thanks for your blog post by the way, I'm going to read this immediately and see if I can use it to improve my PR).

lemoinem avatar Jun 06 '16 20:06 lemoinem