Skip to content

Add markdown extension mechanism and syntax #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from

Conversation

rumkin
Copy link
Contributor

@rumkin rumkin commented Nov 11, 2016

I've added markdown extension mechanism. It uses plugins with such syntax:

@plugin()
    Plugin data block
    ...
    is indentation based
    so it could include any data
    like csv, xml and even markdown itself

Examples:

@youtube(Hy-sVByUHqE)

@csv(Id, Name)
    1, Alice
    2, Bob

@uml(sequence)
    Alice->Bob: Hi, Bob.
    Bob->Alice: Hi, Alice.

@datetime()

@iframe(src)

Plugins are defined with Renderer:

const marked = require('marked');
let renderer = new marked.Renderer({
    plugins: {
        video(params, block) {
              return `<video src="${params}"/>`; 
        }
    },
});
let html = marked.parse('...', {plugins: true, renderer});

By default if plugin not found it should be ignored.

@rumkin rumkin mentioned this pull request Nov 11, 2016
@OverFlow636
Copy link

using this on my fork and it works great!

@jHoldroyd
Copy link

jHoldroyd commented Feb 5, 2017

Just tried this out and its great but there are a few issues.

Firstly it throws Cannot read property 'replace' of undefined on line 204 of lib/marked.js which seems to be triggered when you have tags without a line break under them due to it looking for block content.

The second issue we've run into is that if you have a number of the same tags after each other it "eats" them. That is to say without block content under each of them they disappear. An example is:

@link(small, tag, Inventory, /inventory)

@link(small, shopping-cart, Cart, /cart)

Will only show the first link and if there were additional items under the second one, in our case 8 in total it would still only show the first one.

Apart from these issues the system works really well and if I get a chance I'll look into a resolution.

Edit: Changing line 204 to the same as below seems to fix both issues:

block: ( cap[4] !== undefined ? cap[4].replace(new RegExp('^' + cap[4].match(/^\s+/)[0], 'gm'), '') : undefined ),

@rumkin rumkin changed the title Add markdown extension with plugin Add markdown extension mechanism and syntax Feb 5, 2017
* Fix rule processing in code @jHoldroyd
* Add example
* Rewrite tests from ECMA2017 to ECMA 5
@rumkin
Copy link
Contributor Author

rumkin commented Feb 5, 2017

@jHoldroyd Thanks. Fixed this.

@naivefun
Copy link

naivefun commented Feb 8, 2017

Nice. Maybe add a plugin examples folder for demo?

@rumkin
Copy link
Contributor Author

rumkin commented Feb 10, 2017

@naivefun Do you expect to see exact plugin example?

@naivefun
Copy link

@rumkin actually it is very straightforward, but still if there are very common use cases like @youtube, @vimeo, even @tweet, that would be great for beginners to learn.

@rumkin
Copy link
Contributor Author

rumkin commented Feb 13, 2017

@naivefun Done.

@jHoldroyd
Copy link

jHoldroyd commented Feb 22, 2017

Just following up on the previous problem we reported, happy to say the initial error has been resolved since you pulled in my suggestion, the "eating" of tags is still occasionally happening though.

I managed to get around it by updating the plugin regex on line 98-100 to:

block.extra = {
  plugin: /^@!?([A-Za-z0-9_-]+) *\( *((.+) *)?\).*?\n((\s+){2,}.*\n(((\5).*\n)|\n)*)?/
};

This allows the script to catch all indented text below a plugin tag but nothing that isn't nested. An example of this fix can be seen here: https://regex101.com/r/76YMYB/1 which you can compare to the original regex pattern here: https://regex101.com/r/QjLRlL/1

Hope this helps.

@rumkin
Copy link
Contributor Author

rumkin commented Mar 3, 2017

@jHoldroyd There is one issue with that regexp it will not match single space.

@text()
 Hi there

This will not be matched, it mean that single tab would not be matched too. So I've replaced (\s+){2,} with (\s{2,}|\t{1,}), this is updated regex.

Thanks for help.

@rumkin
Copy link
Contributor Author

rumkin commented Mar 9, 2017

I realise no-plugin block which should return html/text if plugin is not defined:

@!plugin()
  There is no plugin "plugin".

@!youtube()
   Video on <a href="http://youtube.com/watch?v=Hy-sVByUHqE">Youtube</a>.

It currently doesn't support markdown itself.

@joshbruce
Copy link
Member

Closing due to merge conflicts.

See #1026 as well.

@joshbruce joshbruce closed this Jan 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants