Skip to content

Commit 1905256

Browse files
refactor: migrate result page to ESM imports + some simplifications
1 parent 45972da commit 1905256

3 files changed

Lines changed: 52 additions & 50 deletions

File tree

website/result-page.js

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// eslint-disable-next-line import-x/no-unresolved
2+
import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
3+
14
function normalizeModuleSlug(text) {
25
return text
36
.replaceAll(" ", "-")
@@ -17,6 +20,20 @@ function parseModuleHeadingText(line) {
1720
return plainHeadingMatch ? plainHeadingMatch[1].trim() : "";
1821
}
1922

23+
function collectLinesUntil(lines, startIndex, isEndLine) {
24+
const collected = [];
25+
26+
for (let lineIndex = startIndex; lineIndex < lines.length; lineIndex += 1) {
27+
const line = lines[lineIndex];
28+
if (isEndLine(line)) {
29+
break;
30+
}
31+
collected.push(line);
32+
}
33+
34+
return collected;
35+
}
36+
2037
function extractGeneralNotesMarkdown(lines) {
2138
const startIndex = lines.findIndex(
2239
line => line.trim() === "## General notes"
@@ -25,40 +42,33 @@ function extractGeneralNotesMarkdown(lines) {
2542
return [];
2643
}
2744

28-
const notes = [];
29-
for (let lineIndex = startIndex + 1; lineIndex < lines.length; lineIndex += 1) {
30-
if (/^##\s+/u.test(lines[lineIndex])) {
31-
break;
32-
}
33-
notes.push(lines[lineIndex]);
34-
}
35-
36-
return notes;
45+
return collectLinesUntil(
46+
lines,
47+
startIndex + 1,
48+
line => /^##\s+/u.test(line)
49+
);
3750
}
3851

3952
function extractModuleSectionMarkdown(lines, moduleSlug) {
40-
let startIndex = -1;
41-
42-
for (const [lineIndex, line] of lines.entries()) {
43-
if (line.startsWith("### ")) {
44-
const headingText = parseModuleHeadingText(line);
45-
if (normalizeModuleSlug(headingText) === moduleSlug) {
46-
startIndex = lineIndex;
47-
break;
48-
}
53+
const startIndex = lines.findIndex((line) => {
54+
if (!line.startsWith("### ")) {
55+
return false;
4956
}
50-
}
57+
58+
const headingText = parseModuleHeadingText(line);
59+
return normalizeModuleSlug(headingText) === moduleSlug;
60+
});
5161

5262
if (startIndex === -1) {
5363
return null;
5464
}
5565

66+
const relativeEndIndex = lines
67+
.slice(startIndex + 1)
68+
.findIndex(line => line.startsWith("### "));
5669
let endIndex = lines.length;
57-
for (let lineIndex = startIndex + 1; lineIndex < lines.length; lineIndex += 1) {
58-
if (lines[lineIndex].startsWith("### ")) {
59-
endIndex = lineIndex;
60-
break;
61-
}
70+
if (relativeEndIndex !== -1) {
71+
endIndex = startIndex + 1 + relativeEndIndex;
6272
}
6373

6474
return {
@@ -77,12 +87,13 @@ function buildFilteredModuleMarkdown(markdownText, moduleSlug) {
7787

7888
const generalNotes = extractGeneralNotesMarkdown(lines);
7989
const moduleHeadingLine = moduleSection.headingLine.replace(/^###\s+/u, "## ");
80-
const outputLines = [moduleHeadingLine, ""];
90+
const outputLines = [];
8191

8292
if (generalNotes.length > 0) {
8393
outputLines.push("### General notes", "", ...generalNotes, "");
8494
}
8595

96+
outputLines.push(moduleHeadingLine, "");
8697
outputLines.push("### Findings", "", ...moduleSection.bodyLines);
8798

8899
return {
@@ -95,18 +106,18 @@ function setPageTitle(title) {
95106
document.title = `${title} · Module hints`;
96107
}
97108

109+
function setFullHintsListLink(fullResultsLink) {
110+
fullResultsLink.href = "result.html";
111+
fullResultsLink.textContent = "Full hints list";
112+
}
113+
98114
async function loadAndDisplayMarkdown() {
99115
const markdownContainer = document.getElementById("markdown-container");
100116
const markdownFile = "result.md";
101117
const moduleSlug = new URLSearchParams(window.location.search).get(
102118
"module"
103119
);
104120
const fullResultsLink = document.getElementById("full-results-link");
105-
const markedParser = window.marked?.parse;
106-
107-
if (typeof markedParser !== "function") {
108-
throw new Error("Marked parser is not available");
109-
}
110121

111122
if (!moduleSlug) {
112123
fullResultsLink.remove();
@@ -127,21 +138,19 @@ async function loadAndDisplayMarkdown() {
127138
moduleSlug
128139
);
129140
if (filtered) {
130-
markdownContainer.innerHTML = markedParser(filtered.markdown);
141+
markdownContainer.innerHTML = marked.parse(filtered.markdown);
131142
setPageTitle(filtered.title);
132-
fullResultsLink.href = "result.html";
133-
fullResultsLink.textContent = "Full hints list";
143+
setFullHintsListLink(fullResultsLink);
134144
}
135145
else {
136146
markdownContainer.innerHTML
137147
= "<p>No hints found for this module.</p>";
138148
setPageTitle("Module hints");
139-
fullResultsLink.href = "result.html";
140-
fullResultsLink.textContent = "Full hints list";
149+
setFullHintsListLink(fullResultsLink);
141150
}
142151
}
143152
else {
144-
markdownContainer.innerHTML = markedParser(markdownText);
153+
markdownContainer.innerHTML = marked.parse(markdownText);
145154
}
146155

147156
addHeadingAnchors();
@@ -179,4 +188,4 @@ function addHeadingAnchors() {
179188
}
180189
}
181190

182-
window.onload = loadAndDisplayMarkdown;
191+
loadAndDisplayMarkdown();

website/result.css

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ body {
5151
line-height: 1;
5252
text-decoration: none;
5353
border: 1px solid transparent;
54+
background: linear-gradient(135deg, #7b7b7b, #616161);
55+
color: #fff;
56+
border-color: #a9a9a9;
5457
box-shadow: 0 8px 20px rgb(0 0 0 / 18%);
5558
transition:
5659
transform 140ms ease,
@@ -63,6 +66,7 @@ body {
6366
.action-button:hover {
6467
text-decoration: none;
6568
transform: translateY(-1px);
69+
background: linear-gradient(135deg, #8a8a8a, #707070);
6670
box-shadow: 0 10px 24px rgb(0 0 0 / 26%);
6771
}
6872

@@ -71,16 +75,6 @@ body {
7175
outline-offset: 3px;
7276
}
7377

74-
.action-button {
75-
background: linear-gradient(135deg, #7b7b7b, #616161);
76-
color: #fff;
77-
border-color: #a9a9a9;
78-
}
79-
80-
.action-button:hover {
81-
background: linear-gradient(135deg, #8a8a8a, #707070);
82-
}
83-
8478
#page-eyebrow {
8579
margin: 0;
8680
color: #a9a9a9;
@@ -120,7 +114,7 @@ h3 > a {
120114
color: var(--color-link);
121115
}
122116

123-
a:hover {
117+
#markdown-container a:hover {
124118
text-decoration: underline;
125119
}
126120

website/result.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<title>Result of the module analysis</title>
77
<link rel="icon" type="image/x-icon" href="favicon.svg" />
88
<link rel="stylesheet" href="result.css?v=__ASSET_VERSION__" />
9-
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
109
</head>
1110
<body>
1211
<header id="page-header">
@@ -30,6 +29,6 @@
3029
<main id="page-content">
3130
<div id="markdown-container"></div>
3231
</main>
33-
<script src="result-page.js?v=__ASSET_VERSION__"></script>
32+
<script src="result-page.js?v=__ASSET_VERSION__" type="module"></script>
3433
</body>
3534
</html>

0 commit comments

Comments
 (0)