Skip to content

Commit

Permalink
Add basic HTML API & documentation
Browse files Browse the repository at this point in the history
The Toolbar will now be able to read a `data-label` attribute
and add it to the code snippet.
  • Loading branch information
mAAdhaTTah committed Oct 8, 2016
1 parent aa7bcd3 commit 02d3a36
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
16 changes: 12 additions & 4 deletions plugins/toolbar/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ <h2>Toolbar</h2>

<section>
<h1>How to use</h1>
<p>The Toolbar plugin allows for two methods to register your button, using the <code>Prism.plugins.toolbar.registerButton</code> function.</p>
<p>The Toolbar plugin allows for several methods to register your button, using the <code>Prism.plugins.toolbar.registerButton</code> function.</p>

<p>The simplest method is to pass in an object with a <code>text</code> property string and an optional
<code>onClick</code> function, which when provided, will be called when the button is click.</p>
<p>The simplest method is through the HTML API. Add a <code>data-label</code> attribute to the <code>pre</code> element, and the Toolbar
plugin will read the value of that attribute and append a label to the code snippet.</p>

<pre><code class="language-markup" data-label="Hello World!">&lt;pre data-src=&quot;plugins/show-language/prism-show-language.js&quot; data-label=&quot;Hello World!&quot;&gt;&lt;/pre&gt;</code></pre>

<p>For more flexibility, the Toolbar exposes a JavaScript function that can be used to register new buttons or labels to the Toolbar,
<code>Prism.plugins.toolbar.registerButton</code>.</p>

<p>The function can accept an object with a <code>text</code> property string and an optional
<code>onClick</code> function, which when provided, will be called when the button is clicked.</p>

<pre><code class="language-javascript">Prism.plugins.toolbar.registerButton({
text: 'Hello World!', // required
Expand All @@ -39,7 +47,7 @@ <h1>How to use</h1>

<p>See how the above code registers the <code>Hello World!</code> button? You can use this in your plugins to register your own buttons with the toolbar.</p>

<p>If you need more control, you can provide a callback to <code>registerButton</code> that returns either a <code>span</code> or <code>a</code> element.</p>
<p>If you need more control, you can provide a function to <code>registerButton</code> that returns either a <code>span</code> or <code>a</code> element.</p>

<pre><code class="language-javascript">Prism.plugins.toolbar.registerButton(function() {
var button = document.createElement('a');
Expand Down
20 changes: 19 additions & 1 deletion plugins/toolbar/prism-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @param {Object|Function} opts
*/
Prism.plugins.toolbar.registerButton = function (opts) {
var registerButton = Prism.plugins.toolbar.registerButton = function (opts) {
var callback;

if (typeof opts === 'function') {
Expand Down Expand Up @@ -75,6 +75,24 @@
pre.appendChild(toolbar);
};

registerButton(function(env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
return;
}

if (!pre.hasAttribute('data-label')) {
return;
}

var text = pre.getAttribute('data-label');

var element = document.createElement('span');
element.textContent = text;

return element;
});

/**
* Register the toolbar with Prism.
*/
Expand Down
2 changes: 1 addition & 1 deletion plugins/toolbar/prism-toolbar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02d3a36

Please sign in to comment.