Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnoweb): source code viewer improvements #2757

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gno.land/pkg/gnoweb/static/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,15 @@ code {
margin-left: 1.5rem;
}

.not-selectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

/*** HLJS ***/

/* Copyright (c) 2006, Ivan Sagalaev.
Expand Down
25 changes: 24 additions & 1 deletion gno.land/pkg/gnoweb/static/js/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,35 @@ function renderUsernames(raw) {
return raw.replace(/( |\n)@([_a-z0-9]{5,16})/, "$1[@$2](/r/demo/users:$2)");
}

// Create text nodes for the quotes with class names
const createQuoteNode = (quote) => {
const node = document.createElement("a");
node.textContent = quote;
node.className = "hljs-string";
return node;
};

function parseContent(source, isCode) {
if (isCode) {
const highlightedCode = hljs.highlightAuto(source).value;

// Split the highlighted code into lines
const lines = highlightedCode.split('\n');

// Add line numbers to each line
const numberedLines = lines.map((line, index) => {
return `<span class="not-selectable">${index + 1} </span>${line}`;
});

// Join the lines back into a single string
const numberedCode = numberedLines.join('\n');

const parser = new DOMParser();
const doc = parser.parseFromString(numberedCode, "text/html");

const codeElement = document.createElement("code");
codeElement.classList.add("hljs");
codeElement.innerHTML = highlightedCode;
codeElement.innerHTML = doc.body.innerHTML;

const preElement = document.createElement("pre");
preElement.appendChild(codeElement);
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/views/funcs.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
const extension = window.location.pathname.split(".").pop();
const isCode = key === "package_render" && extension !== "md";

const parsed = parseContent(document.getElementById("source").innerHTML, isCode);
const parsed = parseContent(document.getElementById("source").innerText, isCode);
el.innerHTML = DOMPurify.sanitize(parsed, { USE_PROFILES: { html: true } });
}
}
Expand Down