Skip to content

Commit

Permalink
feat: add button to copy code blocks (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Nov 23, 2021
1 parent 26a710b commit c59f179
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 11 deletions.
7 changes: 4 additions & 3 deletions exampleSite/content/posts/advanced/includes.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ This method can be used to include source code files and keep them automatically
{{</* include file="config.yaml" language="yaml" options="linenos=table,hl_lines=5-6,linenostart=100" */>}}
```

**Code Include:**

<!-- prettier-ignore-start -->
<!-- spellchecker-disable -->
{{< include file="config.yaml" language="yaml" options="linenos=table,hl_lines=5-6,linenostart=100">}}
Expand All @@ -77,9 +79,8 @@ HTML content will be filtered by the `safeHTML` filter and added to the rendered
In some situations, it can be helpful to include Markdown files that also contain shortcodes. While the [default method](#markdown-file-default) works fine to render plain Markdown, shortcodes are not parsed. The only way to get this to work is to use Hugo pages. There are several ways to structure these include pages, so whatever you do, keep in mind that Hugo needs to be able to render and serve these files as regular pages! How it works:

1. First you need to create a directory **within** your content directory. For this example site `_includes` is used.
2. To prevent the theme from embedding the page in the navigation, create a file `_includes/_index.md` and add `GeekdocHidden: true` to the front matter.
3. Place your Markdown files within the `_includes` folder e.g. `/_includes/include-page.md`. Make sure to name it `*.md`.
4. Include the page using `{{</* include file="/_includes/include-page.md" */>}}`.
2. Place your Markdown files within the `_includes` folder e.g. `/_includes/include-page.md`. Make sure to name it `*.md`.
3. Include the page using `{{</* include file="/_includes/include-page.md" */>}}`.

Resulting structure should look like this:

Expand Down
8 changes: 4 additions & 4 deletions exampleSite/content/posts/features/code-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ There are several ways to add code blocks. Most of them work out of the box, onl

To display an inline shortcode use single quotes:

```plain
```Plain
`some code`
```

Expand All @@ -26,7 +26,7 @@ To display an inline shortcode use single quotes:

Code blocks can be uses without language specification:

````markdown
````Markdown
```Plain
some code
```
Expand All @@ -40,7 +40,7 @@ some code

... or if you need language specific syntax highlighting:

````markdown
````Markdown
```Shell
# some code
echo "Hello world"
Expand Down Expand Up @@ -80,7 +80,7 @@ pygmentsCodeFences: true
You can use it like every other shortcode:
<!-- prettier-ignore -->
```markdown
```Markdown
{{</* highlight Shell "linenos=table" */>}}
# some code
echo "Hello World"
Expand Down
2 changes: 2 additions & 0 deletions layouts/partials/foot.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
<script defer src="{{ index .Site.Data.assets "js/clipboard.min.js" | relURL }}"></script>
<script defer src="{{ index .Site.Data.assets "js/clipboard-loader.min.js" | relURL }}"></script>
{{ end }}

<script defer src="{{ index .Site.Data.assets "js/copycode.min.js" | relURL }}"></script>
2 changes: 1 addition & 1 deletion layouts/partials/site-footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</section>
{{ end }}
</div>
{{ if (default true .Site.Params.GeekdocBackToTop) }}
{{ if (default true .Site.Params.GeekblogBackToTop) }}
<div class="flex flex-25 justify-end">
<span class="gblog-footer__item text-right">
<a class="gblog-footer__link fake-link" href="#" aria-label="Back to top">
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/site-header.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<a class="gblog-header__link" rel="me" href="{{ .Site.BaseURL }}">
<span class="gblog-brand flex align-center justify-center">
<img class="gblog-brand__img" src="{{ (default "brand.svg" .Site.Params.logo) | relURL }}" alt="">
<span class="gdoc-brand__title">{{ .Site.Title }}</span>
<span class="gblog-brand__title">{{ .Site.Title }}</span>
</span>
{{ with .Site.Params.subtitle }}
<span class="gblog-brand__subtitle flex align-center justify-center">{{ . }}</span>
Expand Down
2 changes: 1 addition & 1 deletion layouts/shortcodes/include.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{ $language := .Get "language" }}
{{ $options :=.Get "options" }}

<div class="gdoc-include">
<div class="gblog-include">
{{- if (.Get "language") -}}
{{- highlight ($file | readFile) $language (default "linenos=table" $options) -}}
{{- else if eq $type "html" -}}
Expand Down
1 change: 1 addition & 0 deletions src/iconfont/uEA21-check.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/iconfont/uEA22-copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/icons/check.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/icons/copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/js/clipboard-loader.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
document.addEventListener("DOMContentLoaded", function (event) {
var clipboard = new ClipboardJS(".clip");

clipboard.on("success", function (e) {
const trigger = e.trigger;

if (trigger.hasAttribute("data-copy-feedback")) {
trigger.classList.add("gblog-post__codecopy--success");
trigger.querySelector(".icon.copy").classList.add("hidden");
trigger.querySelector(".icon.check").classList.remove("hidden");

setTimeout(function () {
trigger.classList.remove("gblog-post__codecopy--success");
trigger.querySelector(".icon.copy").classList.remove("hidden");
trigger.querySelector(".icon.check").classList.add("hidden");
}, 3000);
}

e.clearSelection();
});
});
34 changes: 34 additions & 0 deletions src/js/copycode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function createCopyButton(highlightDiv) {
const button = document.createElement("span");

if (highlightDiv.querySelector(".lntable")) {
selector = ".lntable .lntd:last-child pre > code";
} else {
selector = "pre > code";
}

const codeToCopy = highlightDiv.querySelector(selector).innerText.trim();

button.classList.add(
"flex",
"align-center",
"justify-center",
"clip",
"gblog-post__codecopy"
);
button.type = "button";
button.innerHTML =
'<svg class="icon copy"><use xlink:href="#gblog_copy"></use></svg>' +
'<svg class="icon check hidden"><use xlink:href="#gblog_check"></use></svg>';
button.setAttribute("data-clipboard-text", codeToCopy);
button.setAttribute("data-copy-feedback", "Copied!");
button.setAttribute("role", "button");
button.setAttribute("aria-label", "Copy");

highlightDiv.classList.add("gblog-post__codecontainer");
highlightDiv.insertBefore(button, highlightDiv.firstChild);
}

document
.querySelectorAll(".highlight")
.forEach((highlightDiv) => createCopyButton(highlightDiv));
40 changes: 40 additions & 0 deletions src/sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,46 @@ img {
font-size: 1.2em;
}
}

&__codecontainer {
position: relative;

&:hover > .gblog-post__codecopy {
visibility: visible;
}
}

&__codecopy {
visibility: hidden;
position: absolute;
top: 0.5rem;
right: 0.5rem;

border: $border-2 solid var(--code-copy-border-color);
border-radius: $border-radius;
background: var(--code-background);
width: 2.2rem;
height: 2.2rem;

.icon {
top: 0;
width: $font-size-20;
height: $font-size-20;
color: var(--code-copy-font-color);
}

&:hover {
cursor: pointer;
}

&--success {
border-color: var(--code-copy-success-color);

.icon {
color: var(--code-copy-success-color);
}
}
}
}

.gblog-footer {
Expand Down
2 changes: 1 addition & 1 deletion src/sass/_utils.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
}

.hidden {
display: none;
display: none !important;
}

.svg-sprite {
Expand Down

0 comments on commit c59f179

Please sign in to comment.