|
| 1 | +--- |
| 2 | +title: Autoloader |
| 3 | +description: Automatically loads the needed languages to highlight the code blocks. |
| 4 | +owner: Golmote |
| 5 | +noCSS: true |
| 6 | +resources: |
| 7 | + - https://dev.prismjs.com/components.js |
| 8 | + - https://prismjs.com/assets/vendor/jszip.min.js |
| 9 | + - https://prismjs.com/assets/vendor/FileSaver.min.js |
| 10 | + - ./script.js |
| 11 | +--- |
| 12 | + |
| 13 | +<style> |
| 14 | + .download-grammars { |
| 15 | + font: inherit; |
| 16 | + border: 0; |
| 17 | + padding: 0; |
| 18 | + margin: 0; |
| 19 | + background: none; |
| 20 | + text-decoration: underline; |
| 21 | + cursor: pointer; |
| 22 | + |
| 23 | + &.loading:after { |
| 24 | + content: " [Generating... " attr(data-progress) "%]"; |
| 25 | + } |
| 26 | + } |
| 27 | +</style> |
| 28 | + |
| 29 | +<section class="language-javascript"> |
| 30 | + |
| 31 | +# How to use |
| 32 | + |
| 33 | +The plugin will automatically handle missing grammars and load them for you. To do this, you need to provide a URL to a directory of all the grammars you want. This can be the path to a local directory with all grammars or a CDN URL. |
| 34 | + |
| 35 | +You can download all the available grammars by clicking on the following link: <button class="download-grammars" type="button">download all grammars</button>.<br /> |
| 36 | +Alternatively, you can also clone the GitHub repo and take the `components` folder from there. Read our [usage section](https://prismjs.com/index.html#basic-usage-cdn) to use a CDN. |
| 37 | + |
| 38 | +You can then download Prism core and any plugins from the [Download](https://prismjs.com/download.html) page, without checking any languages (or just check the languages you want to load by default, e.g. if you're using a language a lot, then you probably want to save the extra HTTP request). |
| 39 | + |
| 40 | +A couple of additional options are available through the configuration object `Prism.plugins.autoloader`. |
| 41 | + |
| 42 | +## Specifying the grammars path |
| 43 | + |
| 44 | +By default, the plugin will look for the missing grammars in the `components` folder. If your files are in a different location, you can specify it using the `languages_path` option: |
| 45 | + |
| 46 | +``` |
| 47 | +Prism.plugins.autoloader.languages_path = 'path/to/grammars/'; |
| 48 | +``` |
| 49 | + |
| 50 | +_Note:_ Autoloader is pretty good at guessing this path. You most likely won't have to change this path. |
| 51 | + |
| 52 | +## Using development versions |
| 53 | + |
| 54 | +By default, the plugin uses the minified versions of the grammars. If you wish to use the development versions instead, you can set the `use_minified` option to false: |
| 55 | + |
| 56 | +``` |
| 57 | +Prism.plugins.autoloader.use_minified = false; |
| 58 | +``` |
| 59 | + |
| 60 | +## Specifying additional dependencies |
| 61 | + |
| 62 | +All default dependencies are already included in the plugin. However, there are some cases where you might want to load an additional dependency for a specific code block. To do so, just add a `data-dependencies` attribute on you `<code>` or `<pre>` tags, containing a list of comma-separated language aliases. |
| 63 | + |
| 64 | +```markup |
| 65 | +<pre><code class="language-pug" data-dependencies="less"> |
| 66 | +:less |
| 67 | + foo { |
| 68 | + color: @red; |
| 69 | + } |
| 70 | +</code><pre> |
| 71 | +``` |
| 72 | + |
| 73 | +## Force to reload a grammar |
| 74 | + |
| 75 | +The plugin usually doesn't reload a grammar if it already exists. In some very specific cases, you might however want to do so. If you add an exclamation mark after an alias in the `data-dependencies` attribute, this language will be reloaded. |
| 76 | + |
| 77 | +```html |
| 78 | +<pre class="language-markup" data-dependencies="markup,css!"><code> |
| 79 | +``` |
| 80 | + |
| 81 | +</section> |
| 82 | + |
| 83 | +<section> |
| 84 | + |
| 85 | +# Examples |
| 86 | + |
| 87 | +Note that no languages are loaded on this page by default. |
| 88 | + |
| 89 | +Basic usage with some Perl code: |
| 90 | + |
| 91 | +```perl |
| 92 | +my ($class, $filename) = @_; |
| 93 | +``` |
| 94 | + |
| 95 | +Alias support with TypeScript's `ts`: |
| 96 | + |
| 97 | +```ts |
| 98 | +const a: number = 0; |
| 99 | +``` |
| 100 | + |
| 101 | +The Less filter used in Pug: |
| 102 | + |
| 103 | +```pug |
| 104 | +:less |
| 105 | + foo { |
| 106 | + color: @red; |
| 107 | + } |
| 108 | +``` |
| 109 | + |
| 110 | +# Markdown |
| 111 | + |
| 112 | +Markdown will use the Autoloader to automatically load missing languages. |
| 113 | + |
| 114 | +````markdown |
| 115 | +The C# code will be highlighted __after__ the rest of this document. |
| 116 | + |
| 117 | +```csharp |
| 118 | +public class Foo : IBar<int> { |
| 119 | + public string Baz { get; set; } = "foo"; |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +The CSS code will be highlighted with this document because CSS has already been loaded. |
| 124 | + |
| 125 | +```css |
| 126 | +a:hover { |
| 127 | + color: green !important; |
| 128 | +} |
| 129 | +``` |
| 130 | +```` |
| 131 | + |
| 132 | +</section> |
0 commit comments