prism-element
Prism-based syntax highlighting
<prism-highlighter>
Syntax highlighting via Prism.
Place a <prism-highlighter>
in your document, preferably as a direct child of
<body>
. It will listen for syntax-highlight
events on its parent element,
and annotate the code being provided via that event.
The syntax-highlight
event's detail is expected to have a code
property
containing the source to highlight. The event detail can optionally contain a
lang
property, containing a string like "html"
, "js"
, etc.
This flow is supported by <marked-element>
.
See: Documentation, Demo.
Usage
Installation
npm install --save @polymer/prism-element
In an html file
<html>
<body>
<div id="parent">
<prism-highlighter></prism-highlighter>
<div id="output"></div>
</div>
</body>
<script type="module">
import '@polymer/prism-element/prism-highlighter.js';
import '@polymer/prism-element/prism-theme-default.js';
import '@polymer/polymer/lib/elements/custom-style.js';
import {html} from '@polymer/polymer/lib/utils/html-tag.js';
// import prism theme styles
const template = html`
<custom-style>
<style include="prism-theme-default"></style>
</custom-style>
`;
document.head.appendChild(template.contents);
const parent = document.getElementById('parent');
const output = document.getElementById('output');
const ev = new CustomEvent('syntax-highlight');
// enter code and language here
ev.detail = {
code: 'p { background-color: blue }',
lang: 'css',
};
parent.dispatchEvent(ev);
// detail is modified by prism-highlighter
output.innerHTML = ev.detail.code;
</script>
</html>
In a Polymer 3 element
import {PolymerElement, html} from '@polymer/polymer';
import '@polymer/prism-element/prism-element.js';
import '@polymer/prism-element/prism-highlighter.js';
import '@polymer/prism-element/prism-theme-default.js';
class SampleElement extends PolymerElement {
static get template() {
return html`
<style include="prism-theme-default"></style>
<div id="parent">
<prism-highlighter></prism-highlighter>
<div id="output"></div>
</div>
`;
}
connectedCallback() {
super.connectedCallback();
const ev = new CustomEvent('syntax-highlight');
// enter code and language here
ev.detail = {
code: 'p { background-color: blue }',
lang: 'css',
};
this.$.parent.dispatchEvent(ev);
// detail is modified by prism-highlighter
this.$.output.innerHTML = ev.detail.code;
}
}
customElements.define('sample-element', SampleElement);
Contributing
If you want to send a PR to this element, here are the instructions for running the tests and demo locally:
Installation
git clone https://github.com/PolymerElements/prism-element
cd prism-element
npm install
npm install -g polymer-cli
Running the demo locally
polymer serve --npm
open http://127.0.0.1:<port>/demo/
Running the tests
polymer test --npm
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Dependencies
- prismjs#^1.11.0
- @polymer/polymer#^3.0.0
- Released
- 2018-09-14
- Maturity
- IMPORTED
- License
- BSD 3-clause "New" or "Revised" License
Compatibility
- Framework
- Polymer 1.0+
- Polymer 3.0+
- Polymer 2.0+ in 2.1.0
- Browser
- Browser Independent
prism-element - Vaadin Add-on Directory
Prism-based syntax highlightingGitHub Homepage
View on GitHub
Issue tracker
License
Documentation
Online Demo
prism-element version 0.8.0
### Dependencies
* polymer#Polymer/polymer#0.8-preview
* prism#*
prism-element version 0.8.1
### Dependencies
* polymer#Polymer/polymer#v0.8.0-rc.4
* prism#*
prism-element version 0.8.2
### Dependencies
* polymer#Polymer/polymer#v0.8.0-rc.6
* prism#*
prism-element version 0.8.3
### Dependencies
* polymer#Polymer/polymer#v0.8.0-rc.7
* prism#*
prism-element version 0.8.4
### Dependencies
* polymer#Polymer/polymer#v0.8.0-rc.7
* prism#*
prism-element version 0.9.0
### Dependencies
* polymer#Polymer/polymer#v0.9.0-rc.1
* prism#*
prism-element version 0.9.2
### Dependencies
* prism#*
* polymer#Polymer/polymer#^0.9.0
prism-element version 1.0.0
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.0.0
prism-element version 1.0.1
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.0.0
prism-element version 1.0.2
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.0.0
prism-element version 1.0.3
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.0.0
prism-element version 0.0.1
### Dependencies
* prismjs-package#*
* @polymer/polymer#^1.2.5-npm-test.2
prism-element version 0.0.3
### Dependencies
* prismjs-package#*
* @polymer/polymer#^1.2.5-npm-test.2
prism-element version 1.0.4
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.0.0
prism-element version 1.1.0
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.5.0
prism-element version 1.1.1
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.5.0
prism-element version 1.1.2
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.5.0
prism-element version 1.2.0
### Dependencies
* prism#*
* polymer#Polymer/polymer#^1.5.0
prism-element version 2.0.0
### Dependencies
* prism#*
* polymer#Polymer/polymer#1.9 - 2
prism-element version 2.0.1
### Dependencies
* prism#*
* polymer#Polymer/polymer#1.9 - 2
prism-element version 3.0.0-pre.1
### Dependencies
* @polymer/polymer#^3.0.0-pre.1
prism-element version 3.0.0-pre.2
### Dependencies
* @polymer/polymer#^3.0.0-pre.1
prism-element version 3.0.0-pre.3
### Dependencies
* @polymer/polymer#^3.0.0-pre.3
prism-element version 3.0.0-pre.4
### Dependencies
* @polymer/polymer#^3.0.0-pre.4
prism-element version 3.0.0-pre.6
### Dependencies
* @polymer/polymer#^3.0.0-pre.6
prism-element version 3.0.0-pre.7
### Dependencies
* @polymer/polymer#^3.0.0-pre.7
prism-element version 3.0.0-pre.8
### Dependencies
* @polymer/polymer#^3.0.0-pre.7
prism-element version 3.0.0-pre.10
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0-pre.10
prism-element version 2.1.0
### Dependencies
* prism#*
* polymer#Polymer/polymer#1.9 - 2
prism-element version 3.0.0-pre.11
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0-pre.10
prism-element version 3.0.0-pre.12
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#3.0.0-pre.12
prism-element version 3.0.0-pre.13
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0-pre.13
prism-element version 3.0.0-pre.14
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0-pre.13
prism-element version 3.0.0-pre.15
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0-pre.13
prism-element version 3.0.0-pre.16
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0-pre.13
prism-element version 3.0.0-pre.17
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0-pre.13
prism-element version 3.0.0-pre.18
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.19
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.20
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.21
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.22
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.23
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.24
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.25
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0-pre.26
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.0
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0
prism-element version 3.0.1
### Dependencies
* prismjs#^1.11.0
* @polymer/polymer#^3.0.0