pub / stagit-scripts

Building src.jayvii.de with stagit
git clone https://src.jayvii.de/pub/stagit-scripts.git
Home | Log | Files | Exports | Refs | README | RSS

commit 510493ae91262f5a94783cafb86d499972af2f5a
parent 204ca03ea52670143d0e6eabd5ef74c54084aed5
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Mon,  5 Aug 2024 16:31:08 +0200

feat: add javascript to copy codeblocks to clipboard

Diffstat:
Aassets/js/code_to_clipboard.js | 27+++++++++++++++++++++++++++
Mbin/git_update_repo_html.sh | 9++++++++-
2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/assets/js/code_to_clipboard.js b/assets/js/code_to_clipboard.js @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * SPDX-FileCopyrightText: 2024 JayVii <jayvii[AT]posteo[DOT]de> + */ + +/* Decode HTML by creating a text area in DOM and returning its text value */ +function decode(encoded_html) { + var obj = document.createElement("textarea"); + obj.innerHTML = encoded_html; + return obj.value; +} + +/* Extract Codeblock text from HTML */ +function codeblock(selector = 'pre#blob') { + /* Fetch content of code block */ + var code_raw = document.querySelector(selector).innerHTML; + /* Strip line number links */ + var code_encoded = code_raw.replace(/<a.*<\/a>\ /g, ''); + /* Decode codeblock text */ + var code = decode(code_encoded); + return code; +} + +/* Copy to clipboard and alert user */ +function copy_to_clipboard(text) { + navigator.clipboard.writeText(text); + alert("Copied to your clipboard:\n" + text); +} diff --git a/bin/git_update_repo_html.sh b/bin/git_update_repo_html.sh @@ -204,12 +204,19 @@ INJEXP="s/(<h1>)(${3})(<\/h1>)/\1<a\ href=\"\/${2}\/\">${2}<\/a>\ \/\ <a\ href=\ find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \; # Replace "git clone" href -INJEXP="s/(git\ clone)\ <a href=\"(https:\/\/.*?\.git)\">/<a href='javascript:navigator.clipboard.writeText(\"\1 \2\");alert(\"Copied to your clipboard:\\\n\1 \2\");'>\1 /g" +INJEXP="s/(git\ clone)\ <a href=\"(https:\/\/.*?\.git)\">/<a href='javascript:copy_to_clipboard(\"\1 \2\");'>\1 /g" find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \; +# Inject copy-codeblock for file previews (needs to be run inside SHELL) +find "${1}/${2}/${3}/file" -type f -name "*.html" -exec sh -c 'sed -E "s/<p>\s*($(basename {} | sed -e 's/\.html$//')\s*\([^\)]*\))\s*<\/p>/<p><a href=\"javascript:copy_to_clipboard(codeblock());>\1 &#11123;<\/a><\/p>/" -i {}' \; + # Mark menu entries as active if page is opened (needs to be run inside SHELL) find "${1}/${2}/${3}/" -type f -name "*.html" -exec sh -c 'sed -E "s/(href=\"[^\"]*$(basename {})\")/\1\ class=\"active\"/g" -i {}' \; +# Inject javascript reference in HTML +INJEXP="s/(<\/html>)/<script\ async\ src=\"\/assets\/js\/code_to_clipboard.js\"><\/script>\1/" +find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \; + # Linking Assets --------------------------------------------------------------- ln -sf "${1}/assets/style.css" "${1}/${2}/${3}/style.css" ln -sf "${1}/assets/favicon.png" "${1}/${2}/${3}/favicon.png"