| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
|
| 5 |
<meta charset="utf-8"> |
| 6 |
<title>languages - Documentation</title> |
| 7 |
|
| 8 |
|
| 9 |
<meta property="og:title" content="Prism generated API documentation"/> |
| 10 |
<meta property="og:type" content="website"/> |
| 11 |
<meta property="og:image" content="/logo.svg"/> |
| 12 |
<meta property="og:site_name" content="Prism"/> |
| 13 |
<meta property="og:url" content="https://prismjs.com"/> |
| 14 |
|
| 15 |
<script src="scripts/prettify/prettify.js"></script> |
| 16 |
<script src="scripts/prettify/lang-css.js"></script> |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
<link type="text/css" rel="stylesheet" href="styles/prettify.css"> |
| 21 |
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> |
| 22 |
<script src="scripts/nav.js" defer></script> |
| 23 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 24 |
<link rel="icon" type="image/png" href="/favicon.png"/> |
| 25 |
</head> |
| 26 |
<body> |
| 27 |
|
| 28 |
<input type="checkbox" id="nav-trigger" class="nav-trigger" /> |
| 29 |
<label for="nav-trigger" class="navicon-button x"> |
| 30 |
<div class="navicon"></div> |
| 31 |
</label> |
| 32 |
|
| 33 |
<label for="nav-trigger" class="overlay"></label> |
| 34 |
|
| 35 |
<nav > |
| 36 |
|
| 37 |
<input type="text" id="nav-search" placeholder="Search" /> |
| 38 |
|
| 39 |
<h2><a href="index.html">Home</a></h2><h2><a href="https://prismjs.com" class="menu-item" id="website_link" >PrismJS</a></h2><h2><a href="https://github.com/PrismJS/prism" target="_blank" class="menu-item" id="github_link" >GitHub</a></h2><h3>Classes</h3><ul><li><a href="Token.html">Token</a></li></ul><h3>Namespaces</h3><ul><li><a href="Prism.html">Prism</a><ul class='members'><li data-type='member'><a href="Prism.html#.disableWorkerMessageHandler">disableWorkerMessageHandler</a></li><li data-type='member'><a href="Prism.html#.manual">manual</a></li></ul><ul class='methods'><li data-type='method'><a href="Prism.html#.highlight">highlight</a></li><li data-type='method'><a href="Prism.html#.highlightAll">highlightAll</a></li><li data-type='method'><a href="Prism.html#.highlightAllUnder">highlightAllUnder</a></li><li data-type='method'><a href="Prism.html#.highlightElement">highlightElement</a></li><li data-type='method'><a href="Prism.html#.tokenize">tokenize</a></li></ul></li><li><a href="Prism.hooks.html">hooks</a><ul class='methods'><li data-type='method'><a href="Prism.hooks.html#.add">add</a></li><li data-type='method'><a href="Prism.hooks.html#.run">run</a></li></ul></li><li><a href="Prism.languages.html">languages</a><ul class='methods'><li data-type='method'><a href="Prism.languages.html#.extend">extend</a></li><li data-type='method'><a href="Prism.languages.html#.insertBefore">insertBefore</a></li></ul></li></ul><h3>Global</h3><ul><li><a href="global.html#Grammar">Grammar</a></li><li><a href="global.html#GrammarToken">GrammarToken</a></li><li><a href="global.html#HighlightCallback">HighlightCallback</a></li><li><a href="global.html#HookCallback">HookCallback</a></li><li><a href="global.html#TokenStream">TokenStream</a></li></ul> |
| 40 |
</nav> |
| 41 |
|
| 42 |
<div id="main"> |
| 43 |
|
| 44 |
<h1 class="page-title">languages</h1> |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
<section> |
| 53 |
|
| 54 |
<header> |
| 55 |
|
| 56 |
<h2> |
| 57 |
<span class="ancestors"><a href="Prism.html">Prism</a>.</span> |
| 58 |
|
| 59 |
languages |
| 60 |
</h2> |
| 61 |
|
| 62 |
|
| 63 |
</header> |
| 64 |
|
| 65 |
<article> |
| 66 |
|
| 67 |
<div class="container-overview"> |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
<dl class="details"> |
| 72 |
|
| 73 |
|
| 74 |
<dt class="tag-source">Source:</dt> |
| 75 |
<dd class="tag-source"><ul class="dummy"><li> |
| 76 |
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line299">line 299</a> |
| 77 |
</li></ul></dd> |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
</dl> |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
<div class="description usertext"><p>This namespace contains all currently loaded languages and the some helper functions to create and modify languages.</p></div> |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
</div> |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
<h3 class="subsection-title">Methods</h3> |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
<h4 class="name" id=".extend"><span class="type-signature">(static) </span>extend<span class="signature">(id, redef)</span><span class="type-signature"> → {<a href="global.html#Grammar">Grammar</a>}</span></h4> |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
<dl class="details"> |
| 153 |
|
| 154 |
|
| 155 |
<dt class="tag-source">Source:</dt> |
| 156 |
<dd class="tag-source"><ul class="dummy"><li> |
| 157 |
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line336">line 336</a> |
| 158 |
</li></ul></dd> |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
</dl> |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
<div class="description usertext"> |
| 197 |
<p>Creates a deep copy of the language with the given id and appends the given tokens.</p> |
| 198 |
<p>If a token in <code>redef</code> also appears in the copied language, then the existing token in the copied language |
| 199 |
will be overwritten at its original position.</p> |
| 200 |
<h2>Best practices</h2> |
| 201 |
<p>Since the position of overwriting tokens (token in <code>redef</code> that overwrite tokens in the copied language) |
| 202 |
doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to |
| 203 |
understand the language definition because, normally, the order of tokens matters in Prism grammars.</p> |
| 204 |
<p>Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens. |
| 205 |
Furthermore, all non-overwriting tokens should be placed after the overwriting ones.</p> |
| 206 |
</div> |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
<h5>Example</h5> |
| 217 |
|
| 218 |
<pre class="prettyprint"><code>Prism.languages['css-with-colors'] = Prism.languages.extend('css', { |
| 219 |
// Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token |
| 220 |
// at its original position |
| 221 |
'comment': { ... }, |
| 222 |
// CSS doesn't have a 'color' token, so this token will be appended |
| 223 |
'color': /\b(?:red|green|blue)\b/ |
| 224 |
});</code></pre> |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
<h5>Parameters:</h5> |
| 230 |
|
| 231 |
|
| 232 |
<table class="params"> |
| 233 |
<thead> |
| 234 |
<tr> |
| 235 |
|
| 236 |
<th>Name</th> |
| 237 |
|
| 238 |
|
| 239 |
<th>Type</th> |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
<th class="last">Description</th> |
| 246 |
</tr> |
| 247 |
</thead> |
| 248 |
|
| 249 |
<tbody> |
| 250 |
|
| 251 |
|
| 252 |
<tr> |
| 253 |
|
| 254 |
<td class="name"><code>id</code></td> |
| 255 |
|
| 256 |
|
| 257 |
<td class="type"> |
| 258 |
|
| 259 |
|
| 260 |
<span class="param-type">string</span> |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
</td> |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
<td class="description last"><p>The id of the language to extend. This has to be a key in <code>Prism.languages</code>.</p></td> |
| 271 |
</tr> |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
<tr> |
| 276 |
|
| 277 |
<td class="name"><code>redef</code></td> |
| 278 |
|
| 279 |
|
| 280 |
<td class="type"> |
| 281 |
|
| 282 |
|
| 283 |
<span class="param-type"><a href="global.html#Grammar">Grammar</a></span> |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
</td> |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
<td class="description last"><p>The new tokens to append.</p></td> |
| 294 |
</tr> |
| 295 |
|
| 296 |
|
| 297 |
</tbody> |
| 298 |
</table> |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
<h5>Returns:</h5> |
| 316 |
|
| 317 |
|
| 318 |
<div class="param-desc"> |
| 319 |
<p>The new language created.</p> |
| 320 |
</div> |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
<dl class="param-type"> |
| 325 |
<dt> |
| 326 |
Type |
| 327 |
</dt> |
| 328 |
<dd> |
| 329 |
|
| 330 |
<span class="param-type"><a href="global.html#Grammar">Grammar</a></span> |
| 331 |
|
| 332 |
|
| 333 |
</dd> |
| 334 |
</dl> |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
<h4 class="name" id=".insertBefore"><span class="type-signature">(static) </span>insertBefore<span class="signature">(inside, before, insert, root<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {<a href="global.html#Grammar">Grammar</a>}</span></h4> |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
<dl class="details"> |
| 353 |
|
| 354 |
|
| 355 |
<dt class="tag-source">Source:</dt> |
| 356 |
<dd class="tag-source"><ul class="dummy"><li> |
| 357 |
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line421">line 421</a> |
| 358 |
</li></ul></dd> |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
</dl> |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
<div class="description usertext"> |
| 397 |
<p>Inserts tokens <em>before</em> another token in a language definition or any other grammar.</p> |
| 398 |
<h2>Usage</h2> |
| 399 |
<p>This helper method makes it easy to modify existing languages. For example, the CSS language definition |
| 400 |
not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded |
| 401 |
in HTML through <code><style></code> elements. To do this, it needs to modify <code>Prism.languages.markup</code> and add the |
| 402 |
appropriate tokens. However, <code>Prism.languages.markup</code> is a regular JavaScript object literal, so if you do |
| 403 |
this:</p> |
| 404 |
<pre class="prettyprint source lang-js"><code>Prism.languages.markup.style = { |
| 405 |
// token |
| 406 |
}; |
| 407 |
</code></pre> |
| 408 |
<p>then the <code>style</code> token will be added (and processed) at the end. <code>insertBefore</code> allows you to insert tokens |
| 409 |
before existing tokens. For the CSS example above, you would use it like this:</p> |
| 410 |
<pre class="prettyprint source lang-js"><code>Prism.languages.insertBefore('markup', 'cdata', { |
| 411 |
'style': { |
| 412 |
// token |
| 413 |
} |
| 414 |
}); |
| 415 |
</code></pre> |
| 416 |
<h2>Special cases</h2> |
| 417 |
<p>If the grammars of <code>inside</code> and <code>insert</code> have tokens with the same name, the tokens in <code>inside</code>'s grammar |
| 418 |
will be ignored.</p> |
| 419 |
<p>This behavior can be used to insert tokens after <code>before</code>:</p> |
| 420 |
<pre class="prettyprint source lang-js"><code>Prism.languages.insertBefore('markup', 'comment', { |
| 421 |
'comment': Prism.languages.markup.comment, |
| 422 |
// tokens after 'comment' |
| 423 |
}); |
| 424 |
</code></pre> |
| 425 |
<h2>Limitations</h2> |
| 426 |
<p>The main problem <code>insertBefore</code> has to solve is iteration order. Since ES2015, the iteration order for object |
| 427 |
properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave |
| 428 |
differently when keys are deleted and re-inserted. So <code>insertBefore</code> can't be implemented by temporarily |
| 429 |
deleting properties which is necessary to insert at arbitrary positions.</p> |
| 430 |
<p>To solve this problem, <code>insertBefore</code> doesn't actually insert the given tokens into the target object. |
| 431 |
Instead, it will create a new object and replace all references to the target object with the new one. This |
| 432 |
can be done without temporarily deleting properties, so the iteration order is well-defined.</p> |
| 433 |
<p>However, only references that can be reached from <code>Prism.languages</code> or <code>insert</code> will be replaced. I.e. if |
| 434 |
you hold the target object in a variable, then the value of the variable will not change.</p> |
| 435 |
<pre class="prettyprint source lang-js"><code>var oldMarkup = Prism.languages.markup; |
| 436 |
var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... }); |
| 437 |
|
| 438 |
assert(oldMarkup !== Prism.languages.markup); |
| 439 |
assert(newMarkup === Prism.languages.markup); |
| 440 |
</code></pre> |
| 441 |
</div> |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
<h5>Parameters:</h5> |
| 454 |
|
| 455 |
|
| 456 |
<table class="params"> |
| 457 |
<thead> |
| 458 |
<tr> |
| 459 |
|
| 460 |
<th>Name</th> |
| 461 |
|
| 462 |
|
| 463 |
<th>Type</th> |
| 464 |
|
| 465 |
|
| 466 |
<th>Attributes</th> |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
<th class="last">Description</th> |
| 472 |
</tr> |
| 473 |
</thead> |
| 474 |
|
| 475 |
<tbody> |
| 476 |
|
| 477 |
|
| 478 |
<tr> |
| 479 |
|
| 480 |
<td class="name"><code>inside</code></td> |
| 481 |
|
| 482 |
|
| 483 |
<td class="type"> |
| 484 |
|
| 485 |
|
| 486 |
<span class="param-type">string</span> |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
</td> |
| 491 |
|
| 492 |
|
| 493 |
<td class="attributes"> |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
</td> |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
<td class="description last"><p>The property of <code>root</code> (e.g. a language id in <code>Prism.languages</code>) that contains the |
| 505 |
object to be modified.</p></td> |
| 506 |
</tr> |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
<tr> |
| 511 |
|
| 512 |
<td class="name"><code>before</code></td> |
| 513 |
|
| 514 |
|
| 515 |
<td class="type"> |
| 516 |
|
| 517 |
|
| 518 |
<span class="param-type">string</span> |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
</td> |
| 523 |
|
| 524 |
|
| 525 |
<td class="attributes"> |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
</td> |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
<td class="description last"><p>The key to insert before.</p></td> |
| 537 |
</tr> |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
<tr> |
| 542 |
|
| 543 |
<td class="name"><code>insert</code></td> |
| 544 |
|
| 545 |
|
| 546 |
<td class="type"> |
| 547 |
|
| 548 |
|
| 549 |
<span class="param-type"><a href="global.html#Grammar">Grammar</a></span> |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
</td> |
| 554 |
|
| 555 |
|
| 556 |
<td class="attributes"> |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
</td> |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
<td class="description last"><p>An object containing the key-value pairs to be inserted.</p></td> |
| 568 |
</tr> |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
<tr> |
| 573 |
|
| 574 |
<td class="name"><code>root</code></td> |
| 575 |
|
| 576 |
|
| 577 |
<td class="type"> |
| 578 |
|
| 579 |
|
| 580 |
<span class="param-type">Object.<string, any></span> |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
</td> |
| 585 |
|
| 586 |
|
| 587 |
<td class="attributes"> |
| 588 |
|
| 589 |
<optional><br> |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
</td> |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
<td class="description last"><p>The object containing <code>inside</code>, i.e. the object that contains the |
| 601 |
object to be modified.</p> |
| 602 |
<p>Defaults to <code>Prism.languages</code>.</p></td> |
| 603 |
</tr> |
| 604 |
|
| 605 |
|
| 606 |
</tbody> |
| 607 |
</table> |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
<h5>Returns:</h5> |
| 625 |
|
| 626 |
|
| 627 |
<div class="param-desc"> |
| 628 |
<p>The new grammar object.</p> |
| 629 |
</div> |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
<dl class="param-type"> |
| 634 |
<dt> |
| 635 |
Type |
| 636 |
</dt> |
| 637 |
<dd> |
| 638 |
|
| 639 |
<span class="param-type"><a href="global.html#Grammar">Grammar</a></span> |
| 640 |
|
| 641 |
|
| 642 |
</dd> |
| 643 |
</dl> |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
</article> |
| 656 |
|
| 657 |
</section> |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
</div> |
| 665 |
|
| 666 |
<br class="clear"> |
| 667 |
|
| 668 |
<footer> |
| 669 |
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.10</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. |
| 670 |
</footer> |
| 671 |
|
| 672 |
<script>prettyPrint();</script> |
| 673 |
<script src="scripts/polyfill.js"></script> |
| 674 |
<script src="scripts/linenumber.js"></script> |
| 675 |
|
| 676 |
<script src="scripts/search.js" defer></script> |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
<link type="text/css" rel="stylesheet" href="styles/overwrites.css"> |
| 681 |
|
| 682 |
</body> |
| 683 |
</html> |