Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/thingsym/hugo-theme-techdoc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthingsym <thingsym@gmail.com>2020-10-01 11:46:29 +0300
committerthingsym <thingsym@gmail.com>2020-10-01 11:46:29 +0300
commit6a56979d878ede09b21709605752549c3e7fe0d4 (patch)
treecc58ce7cfd3375a2fb9caa7a7290ce077b309f2c
parente7666cd042c8ab7e4a9011a7729c75c0305348e5 (diff)
feat: add Code highlight with clipboard custom shortcode
-rw-r--r--exampleSite/content/sample/custom-shortcodes.md14
-rw-r--r--layouts/partials/head.html1
-rw-r--r--layouts/shortcodes/code.html18
-rw-r--r--src/js/code.js33
-rw-r--r--src/js/main.js1
-rw-r--r--src/scss/_component.scss51
6 files changed, 118 insertions, 0 deletions
diff --git a/exampleSite/content/sample/custom-shortcodes.md b/exampleSite/content/sample/custom-shortcodes.md
index 351096d..8100645 100644
--- a/exampleSite/content/sample/custom-shortcodes.md
+++ b/exampleSite/content/sample/custom-shortcodes.md
@@ -6,6 +6,20 @@ weight: 10
description: "calling custom Shortcodes into your content files."
---
+## Code highlight with clipboard
+
+{{< code file="sample/custom-shortcodes.html" >}}{{</* highlight html */>}}
+<section id="main">
+ <div>
+ <h1 id="title">{{ .Title }}</h1>
+ {{ range .Pages }}
+ {{ .Render "summary"}}
+ {{ end }}
+ </div>
+</section>
+{{</* /highlight */>}}
+{{< /code >}}
+
## Alert panel
{{< panel >}}Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.{{< /panel >}}
diff --git a/layouts/partials/head.html b/layouts/partials/head.html
index a3e22e1..80154b5 100644
--- a/layouts/partials/head.html
+++ b/layouts/partials/head.html
@@ -19,6 +19,7 @@
{{ partial "meta/chroma.html" . -}}
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.easing@1.4.1/jquery.easing.min.js"></script>
+<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script>
<script src="{{"js/bundle.js" | absURL}}"></script>
{{- partial "meta/google-analytics-async.html" . -}}
{{- partial "meta/tag-manager.html" . -}}
diff --git a/layouts/shortcodes/code.html b/layouts/shortcodes/code.html
new file mode 100644
index 0000000..18c834b
--- /dev/null
+++ b/layouts/shortcodes/code.html
@@ -0,0 +1,18 @@
+{{- $file := .Get "file" -}}
+{{- $codeLang := "" -}}
+{{- $suffix := findRE "(\\.[^.]+)$" $file 1 -}}
+{{- with $suffix -}}
+{{- $codeLang = (index . 0 | strings.TrimPrefix ".") -}}
+{{- end -}}
+{{- with .Get "codeLang" -}}{{- $codeLang = . -}}{{- end -}}
+<div class="code" id="{{ $file | urlize}}">
+ {{- with $file -}}
+ <div class="filename">{{.}}</div>
+ {{- end -}}
+ {{- if ne (.Get "copy") "false" -}}
+ <button class="copy-btn copy" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="Copy this code."><i class="fas fa-clipboard"></i> Copy</button>
+ {{- end -}}
+ <div class="code-content" {{ with .Get "download" }}id="{{ . }}"{{ end }}>
+ {{ if .Get "nocode" }}{{ $.Inner }}{{ else }}{{ with $codeLang }}{{- highlight $.Inner . "" | -}}{{ else }}<pre><code>{{- .Inner | string -}}</code></pre>{{ end }}{{ end }}
+ </div>
+</div>
diff --git a/src/js/code.js b/src/js/code.js
new file mode 100644
index 0000000..d3a8503
--- /dev/null
+++ b/src/js/code.js
@@ -0,0 +1,33 @@
+(function() {
+ window.onload = function() {
+ new ClipboardJS('.copy', {
+ target: function(trigger) {
+ return trigger.nextElementSibling;
+ }
+ }).on('success', function(e) {
+ showTooltip(e.trigger, 'Copied!');
+ e.clearSelection();
+ }).on('error', function(e) {
+ console.error('Action:', e.action);
+ console.error('Trigger:', e.trigger);
+ });
+ }
+
+ document.addEventListener('DOMContentLoaded', function(){
+ const btns = document.querySelectorAll('.copy');
+
+ btns.forEach(( el ) => {
+ el.addEventListener('animationend', clearTooltip);
+ });
+ });
+
+ function showTooltip(e, msg) {
+ e.setAttribute('class', 'copy-btn copy tooltipped');
+ e.setAttribute('aria-label', msg);
+ }
+
+ function clearTooltip(e) {
+ e.currentTarget.setAttribute('class', 'copy-btn copy');
+ e.currentTarget.setAttribute('aria-label', 'Copy this code.');
+ }
+})();
diff --git a/src/js/main.js b/src/js/main.js
index 1a4ad0a..4ffb01f 100644
--- a/src/js/main.js
+++ b/src/js/main.js
@@ -2,3 +2,4 @@ require( './sidebar-menu.js' );
require( './keydown-nav.js' );
require( './jquery.backtothetop/jquery.backtothetop.min.js' );
require( './headerlink.js' );
+require( './code.js' );
diff --git a/src/scss/_component.scss b/src/scss/_component.scss
index 2c6f792..8b62200 100644
--- a/src/scss/_component.scss
+++ b/src/scss/_component.scss
@@ -236,3 +236,54 @@ figure > figcaption h4 {
.ais-Pagination-item {
padding: .6rem;
}
+
+.code {
+ @include grid($flex-wrap: wrap);
+ @extend .stack;
+
+ .filename {
+ @include grid-col($col: 9, $flex-shrink: 0, $max-width: 75%);
+ font-size: 80%;
+ color: #666;
+ }
+ .copy-btn {
+ margin-left: auto;
+
+ cursor: pointer;
+ position: relative;
+
+ font-size: 80%;
+ border: solid 1px #ccc;
+ padding: .2rem .6rem;
+ border-radius: .3rem;
+ line-height: 1;
+ margin-bottom: .2rem;
+ outline: none;
+ }
+ .code-content {
+ @include grid-col($col: 12, $flex-shrink: 0, $max-width: 100%);
+
+ .highlight {
+ @extend .stack-reset;
+ }
+ }
+}
+
+.tooltipped::after {
+ content: 'Copied!';
+ background: #555;
+ display: inline-block;
+ color: #fff;
+ border-radius: .4rem;
+ position: absolute;
+ left: 50%;
+ top: -1.8rem;
+ transform: translate(-50%, 0);
+ font-size: .75rem;
+ padding: 4px 10px 6px 10px;
+ animation: fade-tooltip .5s 1s 1 forwards;
+}
+
+@keyframes fade-tooltip {
+ to { opacity: 0; }
+}