1+ // eslint-disable-next-line import-x/no-unresolved
2+ import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js" ;
3+
14function 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+
2037function 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
3952function 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+
98114async 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 ( ) ;
0 commit comments