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

Markdown: Added support for code blocks #1562

Merged
merged 12 commits into from
Dec 1, 2018

Conversation

RunDevelopment
Copy link
Member

@RunDevelopment RunDevelopment commented Sep 11, 2018

This PR adds support for markdown backtick code blocks.

The new code is intentionally written so that we can easily write a plugin which highlights the code block with the given language.
I actually intended that the language does that itself, but that turned out to be really difficult and it lacks certain features, so a plugin it is then.

It now also supports syntax highlighting!

The highlighting is done in two steps:

  1. Add an alias language-**** containing the given language to the code-block token.
    This happens in the after-tokenize hook.
  2. Highlight the code with the wrap hook.

This is to get around the encoding (util.encode) of tokens in Prism.highlight.

By using this procedure we get the correct execution of the before-tokenize, after-tokenize, and wrap hook for all included languages.

Btw: This implementation would have benefitted from #1524.

Example

image

@RunDevelopment RunDevelopment mentioned this pull request Sep 21, 2018
6 tasks
@ripperhe
Copy link

How I use this for Objective-C? What I should write after "```"?

@RunDevelopment
Copy link
Member Author

@ripperhe
Use objectivec after the first tripple backtick.
The language names are the same as you would use after language- in code or pre tags.

@ripperhe
Copy link

ripperhe commented Sep 30, 2018

But it not works for me. When I use it for ruby or bash is OK, only use it for Objective-C is not available.

I have add this to index.html

<script src="//unpkg.com/prismjs/components/prism-objectivec.min.js"></script>

But it not works for me . This is the final result.

https://ripperhe.com/ZYDebugo/#/Guide/quick-login

This is GitHub link

https://github.com/ripperhe/ZYDebugo/blob/master/docs/Guide/quick-login.md

I had add objectivec.

@RunDevelopment
Copy link
Member Author

@ripperhe That's because you are using https://unpkg.com/[email protected]/ which does not include this PR.

@ripperhe
Copy link

ripperhe commented Oct 1, 2018

@RunDevelopment So, how can I get the latest prism code for Objective-C? What should I write in index.html? Please tell the solution, thank you! 🙏

@RunDevelopment
Copy link
Member Author

@ripperhe
This is an open pull request as of right now, which means that until properly reviewed by one of the maintainers, it is not even clear if this PR is going to be included into Prism's code base.
It is also very likely that there will be changes made to this PR during the review process.

I highly suggest you wait until this PR is reviewed and (hopefully) included in the latest release (which might take a few weeks).

If you really need this feature for your website right now, you can do the following:

  1. Download this branch of my fork.
  2. Include the downloaded files into the JS folder of your website.
  3. Change all references to prism-markdown.min.js from https://unpkg.com/prismjs/components/prism-markdown.min.js to path_to_js_folder/prism/components/prism-markdown.min.js.

@ripperhe
Copy link

ripperhe commented Oct 9, 2018

@RunDevelopment OK, I see. Thank you very much. 🙏

@mAAdhaTTah
Copy link
Member

Looks like there are some conflicts here to resolve.


Prism.hooks.add('after-tokenize', function (env) {
if (env.language !== 'markdown')
return;
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure what our style policy is on this, but I generally prefer {} around if blocks like this.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, looks like core uses {} with if blocks. Otherwise, code looks good. Love this improvement.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right!

@mAAdhaTTah
Copy link
Member

Thought for the future: can we integrate w/ the autoloader so you can write Markdown & it'll autoload the languages you need?

@RunDevelopment
Copy link
Member Author

RunDevelopment commented Dec 1, 2018

write Markdown & it'll autoload the languages you need

I also thought about but for that would we need the Autoloader itself to expose the API and find a clever way to highlight the code asynchronously after the markdown pass.
I'll make an issue after this is merged.

@mAAdhaTTah mAAdhaTTah merged commit b0717e7 into PrismJS:master Dec 1, 2018
@RunDevelopment RunDevelopment deleted the markdown-code-blocks branch December 1, 2018 20:15
ggrossetie pushed a commit to ggrossetie/prism that referenced this pull request Mar 11, 2019
It also supports syntax highlighting!

The highlighting is done in two steps:

1. Add an alias `language-****` containing the given language to the `code-block`
token.
    This happens in the `after-tokenize` hook.
2. Highlight the code with the `wrap` hook.

This is to get around the encoding (`util.encode`) of tokens in `Prism.highlight`.

By using this procedure we get the correct execution of the `before-tokenize`,
`after-tokenize`, and `wrap` hook for all included languages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants