Skip to content

Commit

Permalink
Improve snippet display, feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Jul 15, 2022
1 parent 163bc56 commit 92bfc55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
5 changes: 3 additions & 2 deletions js/css/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ $error-color: #e16969;
border: 2px solid $light-border-color;
padding: 1rem;
width: 50%;
overflow: auto;

@include mobile {
width: 100%;
Expand All @@ -279,14 +280,14 @@ $error-color: #e16969;
.correct:before {
color: green;
margin-right: 0.5rem;
content: "";
content: " Correct";
font-size: 1.5rem;
}

.incorrect:before {
color: red;
margin-right: 0.5rem;
content: "";
content: " Incorrect";
font-size: 1.5rem;
}
}
Expand Down
30 changes: 18 additions & 12 deletions js/lib/components/snippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,25 @@ export let snippetToHtml = (snippet: string, language?: string): string => {
snippet = snippet.trim(); // allow quiz authors to have leading/trailing whitespace
let highlighted: string = hljs.highlight(language || "rust", snippet).value;
let lines = splitHljsOutput(highlighted);
let wrapped = lines
.map(line => {
let codeEl = document.createElement("code");
codeEl.appendChild(line);
let numEl = document.createElement("span");
numEl.className = "line-number";
return [numEl, codeEl];
})
.flat();

let pre = document.createElement("pre");
wrapped.forEach(node => {
pre.appendChild(node);
});
if (lines.length > 1) {
let wrapped = lines
.map(line => {
let codeEl = document.createElement("code");
codeEl.appendChild(line);
let numEl = document.createElement("span");
numEl.className = "line-number";
return [numEl, codeEl];
})
.flat();
wrapped.forEach(node => {
pre.appendChild(node);
});
} else if (lines.length == 1) {
pre.appendChild(lines[0]);
}

return pre.outerHTML;
};

Expand Down

0 comments on commit 92bfc55

Please sign in to comment.