Skip to content

Commit 6d45fce

Browse files
Place plugin docs along with their source code (#3930)
1 parent 44429ca commit 6d45fce

File tree

30 files changed

+2619
-0
lines changed

30 files changed

+2619
-0
lines changed

src/plugins/autolinker/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Autolinker
3+
description: Converts URLs and emails in code to clickable links. Parses Markdown links in comments.
4+
owner: LeaVerou
5+
---
6+
7+
<section>
8+
9+
# How to use
10+
11+
URLs and emails will be linked automatically, you don’t need to do anything. To link some text inside a comment to a certain URL, you may use the Markdown syntax:
12+
13+
```markdown
14+
[Text you want to see](https://url-goes-here.com)
15+
```
16+
</section>
17+
18+
<section>
19+
20+
# Examples
21+
22+
## JavaScript
23+
24+
```js
25+
/**
26+
* Prism: Lightweight, robust, elegant syntax highlighting
27+
* MIT license https://www.opensource.org/licenses/mit-license.php/
28+
* @author Lea Verou https://lea.verou.me
29+
* Reach Lea at [email protected] (no, not really)
30+
* And this is [a Markdown link](https://prismjs.com). Sweet, huh?
31+
*/
32+
let foo = 5;
33+
// And a single line comment https://google.com
34+
```
35+
36+
## CSS
37+
38+
```css
39+
@font-face {
40+
src: url(https://lea.verou.me/logo.otf);
41+
font-family: 'LeaVerou';
42+
}
43+
```
44+
45+
## HTML
46+
47+
```html
48+
<!-- Links in HTML, woo!
49+
Lea Verou https://lea.verou.me or, with Markdown, [Lea Verou](https://lea.verou.me) -->
50+
<img src="https://prismjs.com/assets/img/spectrum.png" alt="In attributes too!" />
51+
<p>Autolinking in raw text: https://prismjs.com</p>
52+
```
53+
</section>

src/plugins/autoloader/README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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>

src/plugins/autoloader/demo.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
async function getZip (files, elt) {
2+
let process = async () => {
3+
elt.setAttribute('data-progress', Math.round((i / l) * 100));
4+
if (i < l) {
5+
await addFile(zip, files[i][0], files[i][1]);
6+
i++;
7+
await process();
8+
}
9+
};
10+
11+
let zip = new JSZip();
12+
let l = files.length;
13+
let i = 0;
14+
15+
await process();
16+
17+
return zip;
18+
}
19+
20+
async function addFile (zip, filename, filepath) {
21+
let contents = await getFileContents(filepath);
22+
zip.file(filename, contents);
23+
}
24+
25+
async function getFileContents (filepath) {
26+
let response = await fetch(filepath);
27+
if (!response.ok) {
28+
throw new Error(`HTTP error! status: ${response.status}`);
29+
}
30+
return response.text();
31+
}
32+
33+
document.querySelector('.download-grammars').addEventListener('click', async ({ target }) => {
34+
let btn = target;
35+
if (btn.classList.contains('loading')) {
36+
return;
37+
}
38+
btn.classList.add('loading');
39+
btn.setAttribute('data-progress', 0);
40+
41+
let files = [];
42+
for (let id in components.languages) {
43+
if (id === 'meta') {
44+
continue;
45+
}
46+
let basepath =
47+
'https://dev.prismjs.com/' + components.languages.meta.path.replace(/\{id}/g, id);
48+
let basename = basepath.substring(basepath.lastIndexOf('/') + 1);
49+
files.push([basename + '.js', basepath + '.js']);
50+
files.push([basename + '.min.js', basepath + '.min.js']);
51+
}
52+
53+
let zip = await getZip(files, btn);
54+
btn.classList.remove('loading');
55+
56+
let blob = await zip.generateAsync({ type: 'blob' });
57+
saveAs(blob, 'prism-components.zip');
58+
});

0 commit comments

Comments
 (0)