Repository navigation
Expand file tree
/
Copy pathreader.mjs
More file actions
307 lines (299 loc) · 14.4 KB
/
Copy pathreader.mjs
File metadata and controls
307 lines (299 loc) · 14.4 KB
1
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Adapted from HowToLiveBetter's line-based parser and DOM rendering approach.
// Copyright (c) 2026 eternity4719. MIT; see LICENSE-CODE.
export const CHAPTERS = [
{ id: '01', path: 'book/pl/01-unikaj-przedwczesnej-smierci.md' },
{ id: '02', path: 'book/pl/02-dbaj-o-zdrowie-na-co-dzien.md' },
{ id: '03', path: 'book/pl/03-nie-marnuj-energii.md' },
{ id: '04', path: 'book/pl/04-nie-marnuj-czasu.md' },
{ id: '05', path: 'book/pl/05-nie-marnuj-pieniedzy.md' },
{ id: '06', path: 'book/pl/06-czarna-lista.md' },
{ id: '07', path: 'book/pl/07-jak-zyc-gdy-nie-masz-pieniedzy.md' },
{ id: '08', path: 'book/pl/08-nie-pakuj-sie-w-tarapaty.md' },
{ id: '09', path: 'book/pl/09-czerwone-linie-prawa.md' },
{ id: '10', path: 'book/pl/10-zwiazek-i-malzenstwo-czy-sie-oplaca.md' },
{ id: '11', path: 'book/pl/11-czerwone-linie-it.md' },
{ id: '12', path: 'book/pl/12-przedsiebiorczosc-i-biznes.md' },
{ id: '13', path: 'book/pl/13-sytuacje-nagle.md' },
{ id: '14', path: 'book/pl/14-bezpieczenstwo-kont-i-danych.md' },
{ id: '15', path: 'book/pl/15-wynajem-i-kupno-mieszkania.md' },
{ id: '16', path: 'book/pl/16-zycie-z-choroba-przewlekla.md' },
{ id: '17', path: 'book/pl/17-senior-w-rodzinie.md' },
{ id: '18', path: 'book/pl/18-rodzicielstwo-decyzje-koszty-i-wsparcie.md' },
{ id: '19', path: 'book/pl/19-praca-rozwiazanie-umowy-i-wypadki.md' },
{ id: '20', path: 'book/pl/20-opieka-nad-noworodkiem-i-niemowleciem.md' },
{ id: '21', path: 'book/pl/21-podroz-za-granice-i-bezpieczenstwo.md' },
{ id: '22', path: 'book/pl/22-odpoczynek-rozrywka-i-bezpieczenstwo.md' },
{ id: '23', path: 'book/pl/23-edukacja-kwalifikacje-i-uczenie-sie.md' },
];
export const FIELDS = [
'Status', 'Koszt', 'W praktyce', 'Korzyść i dowody', 'Ocena dowodów',
'Ocena redakcyjna', 'Ograniczenia', 'Źródła', 'Oryginał', 'Weryfikacja',
'Zmiany w adaptacji',
];
const STATES = new Set(['verified', 'needs_verification', 'needs_localization', 'not_applicable']);
export function parseChapter(markdown, expectedId) {
const ids = new Set();
let chapter = null;
let active = false;
let entry = null;
const flush = () => {
if (!entry) return;
if (!STATES.has(entry.fields.Status)) throw new Error('Nieprawidłowy stan porady ' + entry.id);
if (ids.has(entry.id)) throw new Error('Powtórzony numer porady ' + entry.id);
ids.add(entry.id);
const required = entry.fields.Status === 'verified' ? FIELDS : ['Ograniczenia', 'Źródła', 'Oryginał', 'Weryfikacja'];
for (const field of required) {
if (!entry.fields[field]?.trim()) throw new Error('Brak pola ' + field + ' w ' + entry.id);
}
const date = /^(\d{4}-\d{2}-\d{2}); .+/.exec(entry.fields.Weryfikacja)?.[1];
if (!date || !Number.isFinite(Date.parse(date)) || new Date(date).toISOString().slice(0, 10) !== date) {
throw new Error('Brak poprawnej daty i zakresu weryfikacji');
}
if (!entry.fields['Oryginał'].includes(`[${entry.number}]`)) throw new Error('Niezgodny identyfikator oryginału');
if (entry.fields.Status === 'verified' && !/^[ABC] [—-] .+/.test(entry.fields['Ocena dowodów'])) {
throw new Error('Brak oceny dowodów');
}
chapter.entries.push(entry);
entry = null;
};
for (const line of markdown.split(/\r?\n/)) {
let match;
if ((match = /^# ([1-9]\d*)\. (.+)$/.exec(line))) {
if (chapter) throw new Error('Plik musi zawierać jeden rozdział');
chapter = { id: match[1].padStart(2, '0'), title: match[2], entries: [] };
active = true;
} else if (/^#{1,2} /.test(line)) {
flush();
active = false;
} else if ((match = /^### ([1-9]\d*)\. (.+)$/.exec(line))) {
if (!chapter || !active) throw new Error('Pozycja poza rozdziałem');
flush();
entry = {
id: `porada-${Number(chapter.id)}-${match[1]}`,
number: `${chapter.id}.${match[1]}`,
chapterId: chapter.id,
title: match[2],
fields: {},
};
} else if ((match = /^- ([^:]+): (.+)$/.exec(line)) && entry) {
const [, label, value] = match;
if (!FIELDS.includes(label) || label in entry.fields) throw new Error('Nieprawidłowe lub powtórzone pole ' + label);
entry.fields[label] = value;
} else if (entry && line.trim() && !/^<a id="porada-\d+-\d+"><\/a>$/.test(line)) {
throw new Error('Nieobsługiwany wiersz porady ' + entry.id);
}
}
flush();
if (!chapter?.entries.length) throw new Error('Brak rozdziału lub jego pozycji');
if (expectedId && chapter.id !== expectedId) throw new Error('Niezgodny numer rozdziału: ' + chapter.id);
return chapter;
}
export const fold = text => text.toLocaleLowerCase('pl').normalize('NFD').replace(/\p{M}/gu, '').replace(/ł/g, 'l');
export const published = entries => entries.filter(entry => entry.fields.Status === 'verified');
export function safeHref(href) {
if (/^https:\/\//.test(href)) {
try {
const url = new URL(href);
return url.hostname && !url.username && !url.password ? href : null;
} catch { return null; }
}
if (/^#porada-[1-9]\d*-[1-9]\d*$/.test(href)) return href;
const match = /^([^/#]+\.md)(#porada-([1-9]\d*)-[1-9]\d*)$/.exec(href);
if (match && CHAPTERS.some(c => c.path.split('/').at(-1) === match[1] && Number(c.id) === Number(match[3]))) return match[2];
return null;
}
// Only inline links are supported. Raw HTML stays plain text.
export function inline(element, text) {
const links = /\[([^\]]+)\]\(([^()\s]+)\)/g;
let offset = 0;
for (const match of text.matchAll(links)) {
element.append(document.createTextNode(text.slice(offset, match.index)));
const href = safeHref(match[2]);
if (href) {
const link = document.createElement('a');
link.href = href;
link.textContent = match[1];
element.append(link);
} else element.append(document.createTextNode(match[0]));
offset = match.index + match[0].length;
}
element.append(document.createTextNode(text.slice(offset)));
}
function fieldsList(entry, labels) {
const list = document.createElement('dl');
for (const label of labels) {
const term = document.createElement('dt');
term.textContent = label;
const description = document.createElement('dd');
inline(description, entry.fields[label]);
list.append(term, description);
}
return list;
}
async function startReader() {
const status = document.getElementById('status');
const search = document.getElementById('search');
const grade = document.getElementById('grade');
const chapterFilter = document.getElementById('chapter');
const retry = document.getElementById('retry');
const resetButton = document.getElementById('reset');
const controls = [search, grade, chapterFilter, resetButton];
const main = document.getElementById('porady');
const toc = document.getElementById('toc');
const chapterNav = document.getElementById('chapters');
if (window.matchMedia('(max-width: 48rem)').matches) document.getElementById('nav-list').open = false;
let chapters = [];
let cards = [];
let sections = [];
const chapterAnchor = id => 'rozdzial-' + Number(id);
function updateCurrentLinks() {
for (const link of document.querySelectorAll('#toc a, #chapters a')) {
const target = document.getElementById(link.hash.slice(1));
if (link.hash === location.hash && target && !target.hidden) link.setAttribute('aria-current', 'location');
else link.removeAttribute('aria-current');
}
}
function apply() {
if (!chapters.length) return;
const query = fold(search.value.trim());
let visible = 0;
for (const { entry, article, nav, haystack } of cards) {
const match = (!query || haystack.includes(query))
&& (!grade.value || entry.fields['Ocena dowodów'].startsWith(grade.value))
&& (!chapterFilter.value || entry.chapterId === chapterFilter.value);
article.hidden = nav.hidden = !match;
if (match) visible++;
}
for (const { id, section } of sections) section.hidden = !cards.some(c => c.entry.chapterId === id && !c.article.hidden);
status.textContent = `Wyświetlono ${visible} z ${cards.length} opracowanych polskich porad. Wczytane rozdziały: ${chapters.length}.`
+ (visible ? '' : ' Brak wyników. Zmień wyszukiwanie lub filtr.');
updateCurrentLinks();
}
function reset() {
search.value = grade.value = chapterFilter.value = '';
apply();
}
function revealHash() {
updateCurrentLinks();
const card = cards.find(c => '#' + c.entry.id === location.hash);
if (card) {
if (card.article.hidden) reset();
card.article.scrollIntoView();
card.article.querySelector('h3').focus({ preventScroll: true });
return;
}
const target = sections.find(c => '#' + chapterAnchor(c.id) === location.hash);
if (target) {
search.value = grade.value = '';
chapterFilter.value = target.id;
apply();
target.section.scrollIntoView();
target.section.querySelector('h2').focus({ preventScroll: true });
}
}
async function load() {
chapters = []; cards = []; sections = [];
main.replaceChildren(); toc.replaceChildren(); chapterNav.replaceChildren();
main.setAttribute('aria-busy', 'true');
controls.forEach(control => { control.disabled = true; });
retry.hidden = retry.disabled = true;
status.textContent = 'Wczytywanie wszystkich polskich rozdziałów…';
try {
if (location.protocol === 'file:') throw new Error('Użyj lokalnego serwera HTTP zgodnie z README.');
const loaded = await Promise.all(CHAPTERS.map(async ({ id, path }) => {
try {
const response = await fetch(path, { cache: 'no-cache', signal: AbortSignal.timeout(10000) });
if (!response.ok) throw new Error(`Nie można odczytać pliku ${path} (HTTP ${response.status}).`);
return parseChapter(await response.text(), id);
} catch (error) {
const message = error instanceof TypeError || ['AbortError', 'TimeoutError'].includes(error.name)
? 'Nie można pobrać rozdziału. Sprawdź połączenie z internetem (przy lokalnym uruchomieniu także działanie serwera) i spróbuj ponownie.' : error.message;
throw new Error(`Rozdział ${id}: ${message}`);
}
}));
if (!loaded.some(c => published(c.entries).length)) throw new Error('Zbiór nie zawiera zweryfikowanych porad.');
chapterFilter.replaceChildren(new Option('Wszystkie rozdziały', ''));
for (const chapter of loaded) {
const entries = published(chapter.entries);
const label = `${Number(chapter.id)}. ${chapter.title}`;
chapterFilter.add(new Option(label, chapter.id));
const chapterItem = document.createElement('li');
const chapterLink = document.createElement('a');
chapterLink.href = '#' + chapterAnchor(chapter.id);
chapterLink.textContent = `${label} (${entries.length})`;
chapterItem.append(chapterLink);
chapterNav.append(chapterItem);
const section = document.createElement('section');
section.id = chapterAnchor(chapter.id);
const heading = document.createElement('h2');
heading.textContent = label;
heading.tabIndex = -1;
heading.id = section.id + '-tytul';
section.setAttribute('aria-labelledby', heading.id);
section.append(heading);
sections.push({ id: chapter.id, section });
for (const entry of entries) {
const article = document.createElement('article');
article.id = entry.id;
const badge = document.createElement('span');
badge.className = 'badge';
badge.textContent = `Porada ${entry.number} · Dowody ${entry.fields['Ocena dowodów'][0]}`;
const title = document.createElement('h3');
title.tabIndex = -1;
title.textContent = entry.title;
title.id = entry.id + '-tytul';
article.setAttribute('aria-labelledby', title.id);
const practical = document.createElement('p');
practical.className = 'practical';
inline(practical, entry.fields['W praktyce']);
const permalink = document.createElement('a');
permalink.className = 'permalink';
permalink.href = '#' + entry.id;
permalink.textContent = 'Stały link';
permalink.setAttribute('aria-label', 'Stały link do porady ' + entry.number);
article.append(badge, title, permalink, practical);
article.append(fieldsList(entry, ['Koszt', 'Korzyść i dowody', 'Ocena dowodów', 'Ograniczenia', 'Ocena redakcyjna']));
const details = document.createElement('details');
const summary = document.createElement('summary');
summary.textContent = 'Dowody, źródła i adaptacja';
details.append(summary, fieldsList(entry, ['Źródła', 'Oryginał', 'Weryfikacja', 'Zmiany w adaptacji']));
article.append(details);
section.append(article);
const nav = document.createElement('li');
const link = document.createElement('a');
link.href = '#' + entry.id;
link.textContent = entry.number + '. ' + entry.title;
nav.append(link);
toc.append(nav);
cards.push({ entry, article, nav, haystack: fold([label, entry.number, entry.title, ...Object.values(entry.fields)].join('\n')) });
}
main.append(section);
}
chapters = loaded;
controls.forEach(control => { control.disabled = false; });
apply();
revealHash();
} catch (error) {
chapters = []; cards = []; sections = [];
main.replaceChildren(); toc.replaceChildren(); chapterNav.replaceChildren();
status.textContent = 'Nie wczytano kompletnego zbioru. Nie pokazano żadnej jego części. ' + error.message;
retry.hidden = false;
} finally {
main.setAttribute('aria-busy', 'false');
retry.disabled = false;
}
}
search.addEventListener('input', apply);
grade.addEventListener('change', apply);
chapterFilter.addEventListener('change', apply);
document.querySelector('form').addEventListener('submit', event => event.preventDefault());
resetButton.addEventListener('click', () => { reset(); search.focus(); });
retry.addEventListener('click', load);
window.addEventListener('hashchange', revealHash);
document.addEventListener('click', event => {
const link = event.target.closest('a[href^="#porada-"], a[href^="#rozdzial-"]');
if (link && link.getAttribute('href') === location.hash) { event.preventDefault(); revealHash(); }
});
await load();
}
if (typeof document !== 'undefined') startReader();