mirror of
https://github.com/nunocoracao/blowfish.git
synced 2026-01-30 16:31:52 +01:00
feat(codeblock): add title support
This commit is contained in:
@@ -3311,13 +3311,37 @@
|
||||
}
|
||||
}
|
||||
@layer utilities {
|
||||
.prose .chroma {
|
||||
position: static;
|
||||
.highlight-wrapper {
|
||||
position: relative;
|
||||
z-index: 0;
|
||||
display: block;
|
||||
overflow: hidden;
|
||||
border-radius: var(--radius-md);
|
||||
}
|
||||
.prose .highlight:not(:has(table)) .chroma, .prose .highlight > .chroma {
|
||||
--tw-shadow: 0 1px 3px 0 var(--tw-shadow-color, rgb(0 0 0 / 0.1)), 0 1px 2px -1px var(--tw-shadow-color, rgb(0 0 0 / 0.1));
|
||||
box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
|
||||
margin-top: 1.7142857em;
|
||||
margin-bottom: 1.7142857em;
|
||||
}
|
||||
.highlight-wrapper pre, .highlight-wrapper table, .highlight-wrapper div {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
.highlight-wrapper:has(.codeblock-title) pre {
|
||||
border-radius: 0;
|
||||
}
|
||||
.codeblock-title {
|
||||
border-bottom-style: var(--tw-border-style);
|
||||
border-bottom-width: 1px;
|
||||
border-color: rgba(var(--color-neutral-200), 1);
|
||||
padding-inline: calc(var(--spacing) * 4);
|
||||
padding-block: calc(var(--spacing) * 2);
|
||||
&:is(.dark *) {
|
||||
border-color: rgba(var(--color-neutral-800), 1);
|
||||
}
|
||||
background-color: #fff;
|
||||
&:is(.dark *) {
|
||||
background-color: #0d1117;
|
||||
}
|
||||
}
|
||||
.chroma .lntd, .chroma .lntd pre {
|
||||
margin: calc(var(--spacing) * 0);
|
||||
@@ -4033,14 +4057,11 @@ button, [role="button"] {
|
||||
margin-right: calc(var(--spacing) * 0);
|
||||
}
|
||||
}
|
||||
.highlight-wrapper {
|
||||
display: block;
|
||||
}
|
||||
.highlight {
|
||||
position: relative;
|
||||
z-index: 0;
|
||||
}
|
||||
.highlight:hover > .copy-button {
|
||||
.highlight-wrapper:hover > .copy-button {
|
||||
visibility: visible;
|
||||
}
|
||||
.copy-button {
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
/* -- Chroma Highlight -- */
|
||||
/* Background */
|
||||
.prose .chroma {
|
||||
@apply static rounded-md;
|
||||
/* margins for codeblock title */
|
||||
.highlight-wrapper {
|
||||
@apply block relative z-0 overflow-hidden shadow rounded-md;
|
||||
margin-top: 1.7142857em;
|
||||
margin-bottom: 1.7142857em;
|
||||
}
|
||||
|
||||
.prose .highlight:not(:has(table)) .chroma,
|
||||
.prose .highlight > .chroma {
|
||||
@apply shadow;
|
||||
.highlight-wrapper pre,
|
||||
.highlight-wrapper table, .highlight-wrapper div {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.highlight-wrapper:has(.codeblock-title) pre {
|
||||
@apply rounded-none;
|
||||
}
|
||||
|
||||
.codeblock-title {
|
||||
@apply px-4 py-2 border-b border-neutral-200 dark:border-neutral-800;
|
||||
@apply bg-white dark:bg-[#0d1117];
|
||||
}
|
||||
|
||||
/* LineTableTD */
|
||||
|
||||
@@ -94,15 +94,11 @@ button,
|
||||
}
|
||||
|
||||
/* Code Copy */
|
||||
.highlight-wrapper {
|
||||
@apply block;
|
||||
}
|
||||
|
||||
.highlight {
|
||||
@apply relative z-0;
|
||||
}
|
||||
|
||||
.highlight:hover > .copy-button {
|
||||
.highlight-wrapper:hover > .copy-button {
|
||||
@apply visible;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,31 +2,26 @@ var scriptBundle = document.getElementById("script-bundle");
|
||||
var copyText = scriptBundle?.getAttribute("data-copy") || "Copy";
|
||||
var copiedText = scriptBundle?.getAttribute("data-copied") || "Copied";
|
||||
|
||||
function createCopyButton(highlightDiv) {
|
||||
function createCopyButton(highlightWrapper) {
|
||||
const button = document.createElement("button");
|
||||
button.className = "copy-button";
|
||||
button.type = "button";
|
||||
button.ariaLabel = copyText;
|
||||
button.innerText = copyText;
|
||||
button.addEventListener("click", () => copyCodeToClipboard(button, highlightDiv));
|
||||
|
||||
highlightDiv.insertBefore(button, highlightDiv.firstChild);
|
||||
const wrapper = document.createElement("div");
|
||||
wrapper.className = "highlight-wrapper";
|
||||
highlightDiv.parentNode.insertBefore(wrapper, highlightDiv);
|
||||
wrapper.appendChild(highlightDiv);
|
||||
button.addEventListener("click", () => copyCodeToClipboard(button, highlightWrapper));
|
||||
highlightWrapper.insertBefore(button, highlightWrapper.firstChild);
|
||||
}
|
||||
|
||||
async function copyCodeToClipboard(button, highlightDiv) {
|
||||
const codeToCopy = getCodeText(highlightDiv);
|
||||
async function copyCodeToClipboard(button, highlightWrapper) {
|
||||
const codeToCopy = getCodeText(highlightWrapper);
|
||||
|
||||
function fallback(codeToCopy, highlightDiv) {
|
||||
function fallback(codeToCopy, highlightWrapper) {
|
||||
const textArea = document.createElement("textArea");
|
||||
textArea.contentEditable = "true";
|
||||
textArea.readOnly = "false";
|
||||
textArea.className = "copy-textarea";
|
||||
textArea.value = codeToCopy;
|
||||
highlightDiv.insertBefore(textArea, highlightDiv.firstChild);
|
||||
highlightWrapper.insertBefore(textArea, highlightWrapper.firstChild);
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(textArea);
|
||||
const sel = window.getSelection();
|
||||
@@ -34,7 +29,7 @@ async function copyCodeToClipboard(button, highlightDiv) {
|
||||
sel.addRange(range);
|
||||
textArea.setSelectionRange(0, 999999);
|
||||
document.execCommand("copy");
|
||||
highlightDiv.removeChild(textArea);
|
||||
highlightWrapper.removeChild(textArea);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -42,10 +37,10 @@ async function copyCodeToClipboard(button, highlightDiv) {
|
||||
if (result.state == "granted" || result.state == "prompt") {
|
||||
await navigator.clipboard.writeText(codeToCopy);
|
||||
} else {
|
||||
fallback(codeToCopy, highlightDiv);
|
||||
fallback(codeToCopy, highlightWrapper);
|
||||
}
|
||||
} catch (_) {
|
||||
fallback(codeToCopy, highlightDiv);
|
||||
fallback(codeToCopy, highlightWrapper);
|
||||
} finally {
|
||||
button.blur();
|
||||
button.innerText = copiedText;
|
||||
@@ -55,7 +50,10 @@ async function copyCodeToClipboard(button, highlightDiv) {
|
||||
}
|
||||
}
|
||||
|
||||
function getCodeText(highlightDiv) {
|
||||
function getCodeText(highlightWrapper) {
|
||||
const highlightDiv = highlightWrapper.querySelector(".highlight");
|
||||
if (!highlightDiv) return "";
|
||||
|
||||
const codeBlock = highlightDiv.querySelector("code");
|
||||
const inlineLines = codeBlock?.querySelectorAll(".cl"); // linenos=inline
|
||||
const tableCodeCell = highlightDiv?.querySelector(".lntable .lntd:last-child code"); // linenos=table
|
||||
@@ -75,5 +73,5 @@ function getCodeText(highlightDiv) {
|
||||
}
|
||||
|
||||
window.addEventListener("DOMContentLoaded", (event) => {
|
||||
document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv));
|
||||
document.querySelectorAll(".highlight-wrapper").forEach((highlightWrapper) => createCopyButton(highlightWrapper));
|
||||
});
|
||||
|
||||
10
layouts/_default/_markup/render-codeblock.html
Normal file
10
layouts/_default/_markup/render-codeblock.html
Normal file
@@ -0,0 +1,10 @@
|
||||
{{- $title := or .Attributes.title "" -}}
|
||||
{{- $lang := or .Type "text" -}}
|
||||
<div class="highlight-wrapper">
|
||||
{{- with $title -}}
|
||||
<div class="codeblock-title">
|
||||
{{- $title -}}
|
||||
</div>
|
||||
{{- end -}}
|
||||
{{- transform.Highlight .Inner $lang .Options -}}
|
||||
</div>
|
||||
Reference in New Issue
Block a user