Added code highlighting and copy button

Update .gitignore, composer.php, and 15 more files...
This commit is contained in:
2022-11-20 21:22:12 +01:00
parent 30b1c55687
commit 4135ff6398
17 changed files with 2351 additions and 30 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -0,0 +1,49 @@
// Highlights the code blocks when included on a page.
// This command is separated in its own file since highlight.js isn't on every page and because I can't use JS
// in a script element without using an external .js file.
Array.from(document.getElementsByClassName("code")).forEach(eCodeContainer => {
let language = null;
eCodeContainer.classList.forEach(cCodeContainer => {
if(cCodeContainer.startsWith("language-")) {
language = cCodeContainer;
}
});
if(language !== null) {
Array.from(eCodeContainer.children).forEach(eCodeLine => {
if(eCodeLine.classList.contains("code-line")) {
eCodeLine.classList.add(language);
hljs.highlightElement(eCodeLine);
}
});
}
});
// Adding the action to copy the code to elements with the "js-code-copy" class.
// The search works by searching the closest parent with the "code" class or that is a "code" element, and then
// reading each of its children with the "code-line" class.
Array.from(document.getElementsByClassName("js-code-copy")).forEach(eCodeCopyButton => {
let eParentCodeBlock = eCodeCopyButton;
while(eParentCodeBlock != null &&!eParentCodeBlock.classList.contains("code") &&
eParentCodeBlock.nodeName.toLowerCase() !== "code") {
eParentCodeBlock = eParentCodeBlock.parentElement;
}
if(eParentCodeBlock != null) {
let code = "";
Array.from(eParentCodeBlock.children).forEach(eCodeLine => {
if(eCodeLine.classList.contains("code-line")) {
code += eCodeLine.textContent + "\n"
}
});
eCodeCopyButton.onclick = function() {
navigator.clipboard.writeText(code);
};
}
});

View File

@@ -24,6 +24,7 @@
transition-timing-function: cubic-bezier(.47,1.64,.41,.8);
user-select: none;
cursor: pointer;
&.primary {
color: var(--dm-button-primary-text-color);
@@ -92,4 +93,19 @@
}
}
}
&.fold-top-right {
top: 0;
clip-path: polygon(0 0, 100% 0, 100% 100%);
i {
top: 0.3em;
right: 0.3rem;
}
&:hover i {
top: 0.4rem;
right: 0.4rem;
}
}
}

View File

@@ -0,0 +1,3 @@
.code-line {
background: transparent !important;
}

View File

@@ -25,3 +25,6 @@
@import 'halfmoon/element/hr';
@import 'halfmoon/element/image';
@import 'halfmoon/element/video';
/* Highlight.JS */
@import 'highlightjs/fixes';