Below includes how to add code style and copy code block to clipboard. [Ref. Link]
Add code style
-
Configure the config.toml
1 2 3 4 5 6 7 8 9 10 11 12 13 14[markup] [markup.highlight] anchorLineNos = false codeFences = true guessSyntax = false hl_Lines = '' lineAnchors = '' lineNoStart = 1 lineNos = false lineNumbersInTable = true noClasses = false noHl = false style = 'monokailight' tabWidth = 4 -
find suitable code style and place it under the
staticorassetsfolder.1hugo gen chromastyles --style monokailight > static/css/code-style.css -
add the css to main page.
1 2 3 4 5 6 7 8 9 10 11 12<html> <head> . <link href="/css/code-style.css" rel="stylesheet"> . </head> <body> . . . </body> </html>
Add copy code block button
- create a javascript file under
static/js/button.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15function createCopyButton(highlightDiv) { const button = document.createElement("button"); button.className = "copy-code-button"; button.type = "button"; button.innerText = "Copy"; 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); } document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv)); - Add style sheet to
static/css/copy-button.css1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103.highlight-wrapper { display: block; } /* Start: Turn off individual column border, margin, and padding when line numbers are showing */ .highlight-wrapper .lntd pre { padding: 0; } .chroma .lntd pre { border: 0px solid #ccc; } .chroma .lntd:first-child { padding: 7px 7px 7px 10px; margin: 0; } .chroma .lntd:last-child { padding: 7px 10px 7px 7px; margin: 0; } /* End: Turn off individual column border, margin, and padding when line numbers are showing */ .highlight { position: relative; z-index: 0; padding: 0; margin:40px 0 10px 0; border-radius: 4px; } .highlight > .chroma { position: static; z-index: 1; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; padding: 10px; } .copy-code-button { position: absolute; z-index: 2; right: 0; top: -29px; font-size: 13px; font-weight: 700; line-height: 14px; letter-spacing: 0.5px; width: 65px; color: #ffffff; background-color: #000000; border: 1.25px solid #232326; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; white-space: nowrap; padding: 6px 6px 7px 6px; margin: 0 0 0 1px; cursor: pointer; opacity: 0.6; } .copy-code-button:hover, .copy-code-button:focus, .copy-code-button:active, .copy-code-button:active:hover { color: #222225; background-color: #b3b3b3; opacity: 0.8; } .copyable-text-area { position: absolute; height: 0; z-index: -1; opacity: .01; } .chroma [data-lang]:before { position: absolute; z-index: 0; top: -29px; left: 0; content: attr(data-lang); font-size: 13px; font-weight: 700; color: white; background-color: black; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-left-radius: 0; border-bottom-right-radius: 0; padding: 6px 6px 7px 6px; line-height: 14px; opacity: 0.6; position: absolute; letter-spacing: 0.5px; border: 1.25px solid #232326; margin: 0 0 0 1px; } - add the js and css to main page.
1 2 3 4 5 6 7 8 9 10 11 12<html> <head> . <link href="/css/copy-button.css" rel="stylesheet"> . </head> <body> . . <script src="/js/button.js"></script> </body> </html>