版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # markdown-it-prism [![Build Status](https://travis-ci.org/jGleitz/markdown-it-prism.svg?branch=master)](https://travis-ci.org/jGleitz/markdown-it-prism) [![npm version](https://badge.fury.io/js/markdown-it-prism.svg)](https://badge.fury.io/js/markdown-it-prism) [![Bower version](https://badge.fury.io/bo/markdown-it-prism.svg)](https://badge.fury.io/bo/markdown-it-prism)
  2. > [markdown-it](https://github.com/markdown-it/markdown-it) plugin to highlight code blocks using [Prism](http://prismjs.com/)
  3. ## Usage
  4. ```js
  5. const md = require('markdown-it')();
  6. const prism = require('markdown-it-prism');
  7. md.use(prism, options);
  8. ```
  9. The plugin will insert the necessary markup into all code blocks. [Include one of Prism’s stylesheets](http://prismjs.com/#basic-usage) in
  10. your HTML to get highlighted code.
  11. ### Options
  12. The `options` object may contain:
  13. | Name | Description | Default |
  14. |---------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
  15. | `highlightInlineCode` | Whether to highlight inline code. | `false` |
  16. | `plugins` | Array of [Prism Plugins](http://prismjs.com/#plugins) to load. The names to use can be found [here](https://github.com/PrismJS/prism/tree/master/plugins). Please note that some prism plugins (notably line-numbers) rely on the DOM being present and can thus not be used with this package (see [#1](https://github.com/jGleitz/markdown-it-prism/issues/1)). | `[]` |
  17. | `init` | A function called after setting up prism. Will receive the prism instance as only argument. Useful for plugins needing further intialisation. | `() => {}` |
  18. | `defaultLanguageForUnknown` | The language to use for code blocks that specify a language that Prism does not know. No default will be used if this option is `undefined`. | `undefined` |
  19. | `defaultLanguageForUnspecified` | The language to use for code block that do not specify a language. No default will be used if this option is `undefined`. | `undefined` |
  20. | `defaultLanguage` | Shorthand to set both `defaultLanguageForUnknown` and `defaultLanguageForUnspecified` to the same value. | `undefined` |
  21. ### Inline Code
  22. When `highlightInlineCode` is set, inline code will be highlighted just like fenced code blocks are.
  23. To specifiy the language of inline code, add `{language=<your-language>}` after the code segment:
  24. ```markdown
  25. `class Demo { };`{language=cpp}
  26. ```
  27. This syntax is compatible with [markdown-it-attrs](https://github.com/arve0/markdown-it-attrs):
  28. The `language=<x>` part will be stripped, but everything else between `{` and `}` will work
  29. with [markdown-it-attrs](https://github.com/arve0/markdown-it-attrs) as usual.
  30. ## Usage with Webpack
  31. If you want to use this plugin together with [Webpack](https://webpack.js.org/), you need to import all languages you intend to use:
  32. ```javascript
  33. import MarkdownIt from 'markdown-it';
  34. import prism from 'markdown-it-prism';
  35. import "prismjs/components/prism-clike"
  36. import "prismjs/components/prism-java"
  37. function component() {
  38. const md = new MarkdownIt();
  39. md.use(prism);
  40. const element = document.createElement('div');
  41. element.innerHTML = md.render(`
  42. Here is some *code*:
  43. \`\`\`java
  44. public class Test {
  45. public void foo() {}
  46. }
  47. \`\`\`
  48. `);
  49. return element;
  50. }
  51. document.body.appendChild(component());
  52. ```
  53. *Beware*: Prisms languages have dependencies onto each other. You need to import the languages together with their dependencies in the
  54. correct order.