|
20 | 20 |
|
21 | 21 | <section>
|
22 | 22 | <h1>How to use</h1>
|
23 |
| - <p>In addition to including the plugin file with your PrismJS build, ensure <a href="https://clipboardjs.com/">Clipboard.js</a> is loaded before the plugin.</p> |
24 | 23 |
|
25 |
| - <p>The simplest way to include Clipboard.js is to use any of the |
26 |
| - <a href="https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers">recommended CDNs</a>. If you're using Browserify, Clipboard.js will be loaded automatically |
| 24 | + <p>The plugin depends on <a href="https://clipboardjs.com/">clipboard.js</a> and the Prism <a href="https://prismjs.com/plugins/toolbar/">Toolbar</a> plugin. In addition to including the plugin file with your PrismJS build, ensure they are loaded before the plugin.</p> |
| 25 | + |
| 26 | + <p>The simplest way to include clipboard.js is to use any of the |
| 27 | + <a href="https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers">recommended CDNs</a>. If you're using Browserify, clipboard.js will be loaded automatically |
27 | 28 | if it's included in your <code>package.json</code>.
|
28 |
| - If you don't load Clipboard.js yourself, the plugin will load it from a CDN for you.</p> |
| 29 | + If you don't load clipboard.js yourself, the plugin will load it from a CDN for you.</p> |
| 30 | +</section> |
| 31 | + |
| 32 | +<section> |
| 33 | + <h1>Settings</h1> |
| 34 | + |
| 35 | + <p>By default, the plugin shows messages in English and sets a 5-second timeout after a click. You can use the following HTML5 data attributes to override the default settings:</p> |
| 36 | + |
| 37 | + <ul> |
| 38 | + <li><code class="token attr-name">data-prismjs-copy</code> — default message displayed by Copy to Clipboard;</li> |
| 39 | + <li><code class="token attr-name">data-prismjs-copy-error</code> — a message displayed after failing copying, prompting the user to press <code>Ctrl+C</code>;</li> |
| 40 | + <li><code class="token attr-name">data-prismjs-copy-success</code> — a message displayed after a successful copying;</li> |
| 41 | + <li><code class="token attr-name">data-prismjs-copy-timeout</code> — a timeout (in milliseconds) after copying. Once the timeout passed, the success or error message will revert back to the default message. The value should be a non-negative integer.</li> |
| 42 | + </ul> |
| 43 | + |
| 44 | + <p>The plugin traverses up the DOM tree to find each of these attributes. The search starts at every <code class="token tag">pre code</code> element and stops at the closest ancestor element that has a desired attribute or at the worst case, at the <code class="token tag">html</code> element.</p> |
| 45 | + |
| 46 | + <p><strong>Warning!</strong> Although possible, you definitely shouldn't add these attributes to the <code class="token tag">html</code> element, because a human-readable text should be placed <em>after</em> the character encoding declaration (<code><meta charset="..."></code>), and the latter <a href="https://html.spec.whatwg.org/multipage/semantics.html#charset">must be</a> serialized completely within the first 512 (in older browsers) or 1024 bytes of the document. Consider using the <code class="token tag">body</code> element or one of its descendants.</p> |
| 47 | +</section> |
| 48 | + |
| 49 | +<section> |
| 50 | + <h1>Examples</h1> |
| 51 | + |
| 52 | + <h2>Sharing</h2> |
| 53 | + |
| 54 | + <p>The following code blocks show modified messages and both use a half-second timeout. The other settings are set to default.</p> |
| 55 | + |
| 56 | + <p>Source code:</p> |
| 57 | + |
| 58 | + <pre><code class="language-html"><body data-prismjs-copy-timeout="500"> |
| 59 | + <pre><code class="language-js" data-prismjs-copy="Copy the JavaScript snippet!">console.log('Hello, world!');</code></pre> |
| 60 | + |
| 61 | + <pre><code class="language-c" data-prismjs-copy="Copy the C snippet!">int main() { |
| 62 | + return 0; |
| 63 | +}</code></pre> |
| 64 | +</body></code></pre> |
| 65 | + |
| 66 | + <p>Output:</p> |
| 67 | + |
| 68 | + <div data-prismjs-copy-timeout="500"> |
| 69 | + <pre><code class="language-js" data-prismjs-copy="Copy the JavaScript snippet!">console.log('Hello, world!');</code></pre> |
| 70 | + |
| 71 | + <pre><code class="language-c" data-prismjs-copy="Copy the C snippet!">int main() { |
| 72 | + return 0; |
| 73 | +}</code></pre> |
| 74 | + </div> |
| 75 | + |
| 76 | + <h2>Inheritance</h2> |
| 77 | + |
| 78 | + <p>The plugin always use the closest ancestor element that has a desired attribute, so it's possible to override any setting on any descendant. In the following example, the <code class="token attr-value">baz</code> message is used. The other settings are set to default.</p> |
| 79 | + |
| 80 | + <p>Source code:</p> |
| 81 | + |
| 82 | + <pre><code class="language-html"><body data-prismjs-copy="foo"> |
| 83 | + <main data-prismjs-copy="bar"> |
| 84 | + <pre><code class="language-c" data-prismjs-copy="baz">int main() { |
| 85 | + return 0; |
| 86 | +}</code></pre> |
| 87 | + </main> |
| 88 | +</body></code></pre> |
| 89 | + |
| 90 | + <p>Output:</p> |
| 91 | + |
| 92 | + <div data-prismjs-copy="foo"> |
| 93 | + <div data-prismjs-copy="bar"> |
| 94 | + <pre><code class="language-c" data-prismjs-copy="baz">int main() { |
| 95 | + return 0; |
| 96 | +}</code></pre> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + |
| 100 | + <h2>i18n</h2> |
| 101 | + |
| 102 | + <p>You can use the data attributes for internationalization.</p> |
| 103 | + |
| 104 | + <p>The following code blocks use shared messages in Russian and the default 5-second timeout.</p> |
| 105 | + |
| 106 | + <p>Source code:</p> |
| 107 | + |
| 108 | + <pre><code class="language-html"><!DOCTYPE html> |
| 109 | +<html lang="ru"> |
| 110 | +<!-- The head is omitted. --> |
| 111 | +<body |
| 112 | + data-prismjs-copy="Скопировать" |
| 113 | + data-prismjs-copy-error="Нажмите Ctrl+C, чтобы скопировать" |
| 114 | + data-prismjs-copy-success="Скопировано!" |
| 115 | +> |
| 116 | + <pre><code class="language-c">int main() { |
| 117 | + return 0; |
| 118 | +}</code></pre> |
| 119 | + |
| 120 | + <pre><code class="language-js">console.log('Hello, world!');</code></pre> |
| 121 | +</body> |
| 122 | +</html></code></pre> |
| 123 | + |
| 124 | + <p>Output:</p> |
| 125 | + |
| 126 | + <div |
| 127 | + data-prismjs-copy="Скопировать" |
| 128 | + data-prismjs-copy-error="Нажмите Ctrl+C, чтобы скопировать" |
| 129 | + data-prismjs-copy-success="Скопировано!" |
| 130 | + > |
| 131 | + <pre><code class="language-c">int main() { |
| 132 | + return 0; |
| 133 | +}</code></pre> |
| 134 | + |
| 135 | + <pre><code class="language-js">console.log('Hello, world!');</code></pre> |
| 136 | + </div> |
| 137 | + |
| 138 | + <p>The next HTML document is in English, but some code blocks show messages in Russian and simplified Mainland Chinese. The other settings are set to default.</p> |
| 139 | + |
| 140 | + <p>Source code:</p> |
| 141 | + |
| 142 | + <pre><code class="language-html"><!DOCTYPE html> |
| 143 | +<html lang="en"><!-- The head is omitted. --> |
| 144 | +<body> |
| 145 | + <pre><code class="language-js">console.log('Hello, world!');</code></pre> |
| 146 | + |
| 147 | + <pre |
| 148 | + lang="ru" |
| 149 | + data-prismjs-copy="Скопировать" |
| 150 | + data-prismjs-copy-error="Нажмите Ctrl+C, чтобы скопировать" |
| 151 | + data-prismjs-copy-success="Скопировано!" |
| 152 | + ><code class="language-js">console.log('Привет, мир!');</code></pre> |
| 153 | + |
| 154 | + <pre |
| 155 | + lang="zh-Hans-CN" |
| 156 | + data-prismjs-copy="复制文本" |
| 157 | + data-prismjs-copy-error="按Ctrl+C复制" |
| 158 | + data-prismjs-copy-success="文本已复制!" |
| 159 | + ><code class="language-js">console.log('你好,世界!');</code></pre> |
| 160 | +</body> |
| 161 | +</html></code></pre> |
| 162 | + |
| 163 | + <p>Output:</p> |
| 164 | + |
| 165 | + <div> |
| 166 | + <pre><code class="language-js">console.log('Hello, world!');</code></pre> |
| 167 | + |
| 168 | + <pre |
| 169 | + lang="ru" |
| 170 | + data-prismjs-copy="Скопировать" |
| 171 | + data-prismjs-copy-error="Нажмите Ctrl+C, чтобы скопировать" |
| 172 | + data-prismjs-copy-success="Скопировано!" |
| 173 | + ><code class="language-js">console.log('Привет, мир!');</code></pre> |
29 | 174 |
|
30 |
| - <pre data-src="plugins/copy-to-clipboard/prism-copy-to-clipboard.js"></pre> |
| 175 | + <pre |
| 176 | + lang="zh-Hans-CN" |
| 177 | + data-prismjs-copy="复制文本" |
| 178 | + data-prismjs-copy-error="按Ctrl+C复制" |
| 179 | + data-prismjs-copy-success="文本已复制!" |
| 180 | + ><code class="language-js">console.log('你好,世界!');</code></pre> |
| 181 | + </div> |
31 | 182 | </section>
|
32 | 183 |
|
33 | 184 | <footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
|
34 | 185 |
|
35 | 186 | <script src="prism.js"></script>
|
| 187 | +<script src="plugins/autoloader/prism-autoloader.js" data-autoloader-path="components/"></script> |
36 | 188 | <script src="plugins/toolbar/prism-toolbar.js"></script>
|
37 | 189 | <script src="plugins/copy-to-clipboard/prism-copy-to-clipboard.js"></script>
|
38 | 190 | <script src="assets/utopia.js"></script>
|
|
0 commit comments