-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: add copy button to code blocks
- Loading branch information
Showing
13 changed files
with
190 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
+++ | ||
title = "Sense JavaScript obligatori" | ||
date = 2023-01-06 | ||
updated = 2023-04-28 | ||
description = "JavaScript només s'utilitza quan HTML i CSS no són suficients." | ||
|
||
[taxonomies] | ||
tags = ["funcionalitat"] | ||
+++ | ||
|
||
## JavaScript? | ||
|
||
Aquest tema funciona perfectament sense JavaScript. Opcionalment, pot carregar una quantitat mínima per afegir algunes funcionalitats que no són possibles utilitzant només HTML i CSS: | ||
|
||
- **Canvi de mode clar/fosc**. S'activa establint `theme_switcher = true`. (~900 bytes) | ||
- **Còpia de blocs de codi amb un sol clic**. S'activa establint `copy_button = true`. (~700 bytes) | ||
|
||
Aquestes dues configuracions cal aplicar-les a la secció `[extra]` del fitxer `config.toml`. | ||
|
||
La funcionalitat de KaTex, que requereix carregar un fitxer JavaScript de 274 KB, es pot activar per a publicacions específiques. Això es pot fer establint `katex = true` a la secció `[extra]` de l'encapçalament de la publicació. | ||
|
||
A part d'això, és un tema ràpid amb HTML i CSS. Tal i com hauria de ser (la major part de) la web :-) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
+++ | ||
title = "Sin JavaScript obligatorio" | ||
date = 2023-01-06 | ||
updated = 2023-04-28 | ||
description = "JavaScript solo se utiliza cuando HTML y CSS no son suficientes." | ||
|
||
[taxonomies] | ||
tags = ["funcionalidad"] | ||
+++ | ||
|
||
## ¿JavaScript? | ||
|
||
Este tema funciona perfectamente sin JavaScript. Opcionalmente, puede cargar una cantidad mínima para añadir algunas funciones que son imposibles de lograr con HTML y CSS: | ||
|
||
- **El cambio de modo claro/oscuro**. Habilitado estableciendo `theme_switcher = true`. (~900 bytes) | ||
- **Copia de bloques de código con un clic**. Se activa configurando `copy_button = true`. (~700 bytes) | ||
|
||
Estas dos configuraciones se deben aplicar en la sección `[extra]` de tu archivo `config.toml`. | ||
|
||
El soporte de KaTex, que requiere cargar un archivo JavaScript de 274 KB, se puede activar para publicaciones específicas. Esto se puede hacer configurando `katex = true` en la sección `[extra]` del encabezado de la publicación. | ||
|
||
Aparte de esto, es un tema rápido con HTML y CSS. Como debería ser (en su mayoría) la web :-) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
+++ | ||
title = "No mandatory JavaScript" | ||
date = 2023-01-06 | ||
updated = 2023-04-28 | ||
description = "JavaScript is only used when HTML and CSS aren't enough." | ||
|
||
[taxonomies] | ||
tags = ["showcase"] | ||
+++ | ||
|
||
## JavaScript? | ||
|
||
This theme has no mandatory JavaScript. Optionally, it can load a minimal amount to add some features that are impossible to achieve with HTML and CSS: | ||
|
||
- **Light/dark mode switch**. Enabled by setting `theme_switcher = true`. (~900 bytes) | ||
- **One-click copy of code blocks**. Enabled by setting `copy_button = true`. (~700 bytes) | ||
|
||
These two settings can be applied in the `[extra]` section of your `config.toml` file. | ||
|
||
KaTex support, which requires loading a 274 KB JavaScript file, can be activated for specific posts. This can be done by setting `katex = true` in the post's `[extra]` section of the post's front matter. | ||
|
||
Other than that, it's a fast site with HTML and CSS. Just the way (most of) the web should be :-) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const changeIcon = (copyDiv, className) => { | ||
copyDiv.classList.add(className); | ||
setTimeout(() => copyDiv.classList.remove(className), 2500); | ||
}; | ||
|
||
const addCopyEventListenerToDiv = (copyDiv, block) => { | ||
copyDiv.addEventListener("click", () => copyCodeAndChangeIcon(copyDiv, block)); | ||
}; | ||
|
||
const copyCodeAndChangeIcon = async (copyDiv, block) => { | ||
const code = block.querySelector('table') ? getTableCode(block) : getNonTableCode(block); | ||
try { | ||
await navigator.clipboard.writeText(code); | ||
changeIcon(copyDiv, "checked"); | ||
} catch (error) { | ||
changeIcon(copyDiv, "error"); | ||
} | ||
}; | ||
|
||
const getNonTableCode = (block) => { | ||
return [...block.querySelectorAll('code')] | ||
.map(code => code.textContent) | ||
.join(''); | ||
}; | ||
|
||
const getTableCode = (block) => { | ||
return [...block.querySelectorAll('tr')] | ||
.map(row => row.querySelector('td:last-child')?.innerText ?? '') | ||
.join(''); | ||
}; | ||
|
||
document.querySelectorAll("pre").forEach((block) => { | ||
const copyDiv = document.createElement("div"); | ||
copyDiv.className = "copy-code"; | ||
block.prepend(copyDiv); | ||
addCopyEventListenerToDiv(copyDiv, block); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters