Skip to content
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

Added support to adding simple block rules #1003

Closed
wants to merge 2 commits into from
Closed

Added support to adding simple block rules #1003

wants to merge 2 commits into from

Conversation

KostyaTretyak
Copy link
Contributor

@KostyaTretyak KostyaTretyak commented Jan 6, 2018

const marked = require('marked');
const blockStr = `
# Example usage with embed block code

@@@ gist
a9dfd77500990871fc58b97fdb57d91f.js
@@@

@@@ youtube
JgwnkM5WwWE
@@@
`;

marked.setBlockRule(/^@@@ *(\w+)\n([\s\S]+?)\n@@@/, function (execArr) {
  const channel = execArr[1];
  switch (channel) {
    case 'youtube': {
        const id = marked.escape(execArr[2]);
        return `<iframe width="420" height="315" src="https://www.youtube.com/embed/${id}"></iframe>\n`;
     }
    case 'gist': {
        const id = marked.escape(execArr[2]);
        return `<script src="https://gist.github.com/${id}"></script>\n`;
     }
  }
});
const html = marked.parse(blockStr);
console.log(html);

Output:

<h1 id="example-usage-with-embed-block-code">Example usage with embed block code</h1>
<script src="https://gist.github.com/a9dfd77500990871fc58b97fdb57d91f.js"></script>
<iframe width="420" height="315" src="https://www.youtube.com/embed/JgwnkM5WwWE"></iframe>

@joshbruce
Copy link
Member

Thanks @KostyaTretyak! Gonna flag this for milestone 0.5.0. If you could take a look at #1002 - I believe it could cause this to have merge conflicts in the future as we would need to add tests to document and validate the solution works. Cheers.

@joshbruce joshbruce added this to the 0.6.0 - Improve developer experience milestone Jan 6, 2018
@KostyaTretyak
Copy link
Contributor Author

@joshbruce no, it does not seem that my changes were in conflict with the changes introduced in #1002.

lib/marked.js Outdated
for (var i = 0; i < simpleRules.length; i++) {
if (cap = simpleRules[i].exec(src)) {
src = src.substring(cap[0].length);
this.tokens.push({ type: 'simpleRule' + simpleRules.length, cap: cap });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be 'simpleRule' + i ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I'll correct it right now. Thank you.

@joshbruce
Copy link
Member

See #1026

Closing as merge conflicts - also building out the issue above to consolidate ideas and approaches proposed.

@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants