Skip to content

Commit fdb69ba

Browse files
authored
Merge pull request #23 from dunkmann00/octicons
Make copy button code a template
2 parents 93d13c5 + 14dccc4 commit fdb69ba

File tree

3 files changed

+15
-42
lines changed

3 files changed

+15
-42
lines changed

_layouts/default.html

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ <h1><a href="{{ "/" | absolute_url }}">{{ site.title }}</a></h1>
2424
</div>
2525
{% endif %}
2626
</div>
27+
<template id=clipboard_template>
28+
<div>
29+
<clipboard-copy aria-label="Copy" class="btn btn-invisible m-2 p-0 tooltipped-no-delay d-flex flex-justify-center flex-items-center" tabindex="0" role="button">
30+
{% octicon copy height:16 %}
31+
{% octicon check height:16 class:"color-fg-success d-none" %}
32+
</clipboard-copy>
33+
</div>
34+
</template>
2735
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.1.0/anchor.min.js" integrity="sha256-lZaRhKri35AyJSypXXs4o6OPFTbTmUoltBbDCbdzegg=" crossorigin="anonymous"></script>
2836
<script>anchors.add();</script>
2937
<script type="module" src="{{ "/assets/js/primer-clipboard-copy.js?v=" | append: site.github.build_revision | relative_url }}"></script>

assets/js/primer-clipboard-copy.js

+6-42
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,16 @@
11
import './github-clipboard-copy-element.js';
2-
http://localhost:4000/assets/js/github_clipboard_copy_element
3-
String.prototype.format = function () {
4-
var args = arguments;
5-
return this.replace(/{([0-9]+)}/g, function (match, index) {
6-
return typeof args[index] == 'undefined' ? match : args[index];
7-
});
8-
};
9-
10-
function fromHTML(html, trim = true) {
11-
// Process the HTML string.
12-
html = trim ? html.trim() : html;
13-
if (!html) return null;
14-
15-
// Then set up a new template element.
16-
const template = document.createElement('template');
17-
template.innerHTML = html;
18-
const result = template.content.children;
19-
20-
// Then return either an HTMLElement or HTMLCollection,
21-
// based on whether the input HTML had one or more roots.
22-
if (result.length === 1) return result[0];
23-
return result;
24-
}
252

263
function makeCopyButtons() {
27-
const clipboard = `
28-
<div>
29-
<clipboard-copy aria-label="Copy" class="btn btn-invisible m-2 p-0 tooltipped-no-delay d-flex flex-justify-center flex-items-center" value="{0}" tabindex="0" role="button">
30-
<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy">
31-
<path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path>
32-
</svg>
33-
<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-check color-fg-success d-none">
34-
<path d="M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z"></path>
35-
</svg>
36-
</clipboard-copy>
37-
</div>
38-
`
4+
const clipboardTemplate = document.getElementById("clipboard_template");
395

406
const blocks = document.querySelectorAll('div.highlighter-rouge>div.highlight');
417
blocks.forEach((block) => {
42-
// Fix quotes
43-
const sanitized = block.textContent.replaceAll('"', '&quot;')
44-
// Put this blocks content into the clipboard string
45-
const block_clipboard = clipboard.format(sanitized);
46-
// Make an html element from the block clipboard string
47-
const block_clipboard_node = fromHTML(block_clipboard);
8+
//Create DOM node
9+
const clipboardNode = clipboardTemplate.content.cloneNode(true);
10+
// Put this block's content into the clipboard-copy element's value
11+
clipboardNode.querySelector("clipboard-copy").setAttribute("value", block.textContent);
4812
// Add it in to the end of the html code block
49-
block.appendChild(block_clipboard_node);
13+
block.appendChild(clipboardNode);
5014
});
5115
}
5216

jekyll-v4-theme-primer.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
2121
s.add_runtime_dependency "jekyll-github-metadata", "~> 2.16"
2222
s.add_runtime_dependency "jekyll-seo-tag", "~> 2.0"
2323
s.add_runtime_dependency "jekyll-sass-converter", "~> 3.0"
24+
s.add_runtime_dependency "jekyll-octicons", "~> 19.8"
2425
s.add_development_dependency "html-proofer", "~> 3.0"
2526
s.add_development_dependency "rubocop-github", "~> 0.16"
2627
s.add_development_dependency "w3c_validators", "~> 1.3"

0 commit comments

Comments
 (0)