@clarusjs/theme-docs 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clarusjs/theme-docs",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A docs theme for clarusjs: layout with header, sidebar, content and on-this-page, design tokens, dark mode, copy buttons and callouts.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -16,7 +16,7 @@
16
16
  "access": "public"
17
17
  },
18
18
  "peerDependencies": {
19
- "clarusjs": "^0.8.1"
19
+ "clarusjs": "^0.9.0"
20
20
  },
21
21
  "engines": {
22
22
  "node": "^22.12.0 || ^24.0.0 || >=26.0.0"
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^26.6.6",
42
- "clarusjs": "^0.8.1",
42
+ "clarusjs": "^0.9.0",
43
43
  "typescript": "^7.0.2",
44
44
  "vitest": "^5.1.1",
45
45
  "vite": "^8.3.8"
package/src/callout.tscl CHANGED
@@ -1,8 +1,9 @@
1
- @input type: 'tip' | 'info' | 'warning' | 'danger' = 'info'
1
+ @type Kind = 'tip' | 'info' | 'warning' | 'danger'
2
+ @input type: Kind = 'info'
2
3
 
3
4
  <aside role="note" class="callout {{ type }}"><p class="callout-title"><slot name="title">{{ type[0].toUpperCase() + type.slice(1) }}</slot></p><slot /></aside>
4
5
 
5
- <style>
6
+ <style scoped>
6
7
  /* Scoped to this component: :scope is the <aside>; slot content inside it is styled too. */
7
8
  :scope {
8
9
  border-left: 3px solid var(--accent);
package/src/copy.tscl CHANGED
@@ -5,7 +5,7 @@
5
5
  const block = button.closest('.sample, .code-block')?.querySelector('code')
6
6
  // Removed diff lines aren't part of the code to copy.
7
7
  const clone = block?.cloneNode(true)
8
- clone?.querySelectorAll('.diff.remove').forEach((line) => {
8
+ if (clone instanceof Element) clone.querySelectorAll('.diff.remove').forEach((line) => {
9
9
  if (line.nextSibling?.nodeType === 3 && line.nextSibling.textContent === '\n') line.nextSibling.remove() // its newline too
10
10
  line.remove()
11
11
  })
@@ -16,8 +16,9 @@
16
16
  xp = s.xp
17
17
  ready = true
18
18
  for (const link of document.querySelectorAll('.sidebar ul a[href]')) {
19
- if (!lessons.includes(link.getAttribute('href'))) continue
20
- const status = s.status(link.getAttribute('href'))
19
+ const href = link.getAttribute('href') ?? ''
20
+ if (!(link instanceof HTMLElement) || !lessons.includes(href)) continue
21
+ const status = s.status(href)
21
22
  link.dataset.status = status
22
23
  let note = link.querySelector('.status-note')
23
24
  if (!note) {
@@ -31,7 +31,7 @@
31
31
  bar()?.classList.toggle('ok', kind === 'ok')
32
32
  bar()?.classList.toggle('bad', kind === 'bad')
33
33
  }
34
- const steps = () => [...document.querySelectorAll('main .step')]
34
+ const steps = () => [...document.querySelectorAll('main .step')].filter((el) => el instanceof HTMLElement)
35
35
  function show(i, focus) {
36
36
  const all = steps()
37
37
  if (!all.length) return
@@ -40,7 +40,7 @@
40
40
  all.forEach((step, j) => { step.hidden = j !== current })
41
41
  checking = !!all[current].querySelector('[data-quiz]:not([data-answered])')
42
42
  verdict('', '', '')
43
- const id = all[current].getAttribute('aria-labelledby')
43
+ const id = all[current].getAttribute('aria-labelledby') ?? ''
44
44
  name = document.getElementById(id)?.textContent?.replace(/#$/, '').trim() ?? ''
45
45
  document.querySelectorAll('.segments li').forEach((li, j) => {
46
46
  li.className = j < current ? 'done' : j === current ? 'current' : ''
@@ -92,7 +92,8 @@
92
92
  if (typeof document !== 'undefined' && steps().length) {
93
93
  steps().forEach(split)
94
94
  document.body.classList.add('one-step')
95
- document.querySelector('.lesson-bar').hidden = false
95
+ const lessonBar = document.querySelector('.lesson-bar')
96
+ if (lessonBar instanceof HTMLElement) lessonBar.hidden = false
96
97
  // A #hash opens the step it points into and keeps its target: a step heading takes focus, anything else
97
98
  // scrolls into view (Back and Continue go by step headings instead).
98
99
  function reveal() {
@@ -106,10 +107,11 @@
106
107
  }
107
108
  if (!reveal()) show(0, false)
108
109
  document.addEventListener('quiz:result', (event) => {
109
- if (!steps()[current]?.contains(event.target)) return
110
+ if (!(event instanceof CustomEvent) || !(event.target instanceof HTMLElement) || !steps()[current]?.contains(event.target)) return
110
111
  const { ok, done, text: detail } = event.detail
111
112
  if (!done) return verdict('', 'Pick an answer', detail)
112
- record((p) => answer(p, lesson + '#' + event.target.dataset.quiz, ok))
113
+ const quiz = event.target.dataset.quiz
114
+ record((p) => answer(p, lesson + '#' + quiz, ok))
113
115
  checking = !!steps()[current].querySelector('[data-quiz]:not([data-answered])')
114
116
  verdict(ok ? 'ok' : 'bad', ok ? 'Correct!' : 'Not quite', detail)
115
117
  })
package/src/nav.ts CHANGED
@@ -1,10 +1,11 @@
1
- // The theme's settings and the navigation helpers the layout uses. Server-only: the layout is static HTML.
1
+ // The theme's settings and the navigation helpers the layout uses. Server-only: the layout is static HTML. Lists are
2
+ // readonly, as a template's inputs are: the layout passes its config to these helpers as it gets it.
2
3
 
3
4
  /** A sidebar entry; `order` sorts it within its group (lowest first, then by title). */
4
5
  export type Item = { title: string; href: string; order?: number }
5
- export type Group = { title: string; items: Item[] }
6
+ export type Group = { title: string; items: readonly Item[] }
6
7
  /** A top-level part of the docs (Guide, Reference): its own sidebar groups. */
7
- export type Section = { key: string; title: string; href: string; groups: Group[] }
8
+ export type Section = { key: string; title: string; href: string; groups: readonly Group[] }
8
9
  export type Social = { label: string; href: string; icon: 'github' | 'npm' | 'link' }
9
10
  /** A heading on the page, for "On this page". */
10
11
  export type Heading = { id: string; text: string; level: number }
@@ -14,16 +15,16 @@ export type ThemeConfig = {
14
15
  brand: { text: string; accent?: string; logo?: string }
15
16
  /** The site's public address, for structured data (no trailing slash). */
16
17
  siteUrl?: string
17
- sections: Section[]
18
+ sections: readonly Section[]
18
19
  /** Icon links at the right of the header (GitHub, npm…). */
19
- social?: Social[]
20
+ social?: readonly Social[]
20
21
  /**
21
22
  * "Edit this page": a pattern where `:file` is the page's source file under src/pages (`guide/start.md`, found in
22
23
  * `pages`), or a function of the route (the page's source route, for dynamic pages). None without it.
23
24
  */
24
25
  editLink?: string | ((path: string) => string)
25
26
  /** The pages as themeDocs() reads them: source files for `editLink` patterns, and "Last updated" dates. */
26
- pages?: PageInfo[]
27
+ pages?: readonly PageInfo[]
27
28
  /**
28
29
  * Learn: the section whose pages are lessons, and its progress page (not a lesson). Progress, XP and badges are
29
30
  * kept in the browser (localStorage); an XP chip shows in lesson headers and on the section's pages.
@@ -44,24 +45,24 @@ const none: Item = { title: '', href: '' }
44
45
  const flat = (section: Section) => section.groups.flatMap((g) => g.items)
45
46
 
46
47
  /** The section a page belongs to ('' for pages outside every section, like a home page). */
47
- export function sectionKey(sections: Section[], path: string): string {
48
+ export function sectionKey(sections: readonly Section[], path: string): string {
48
49
  return sections.find((s) => flat(s).some((i) => i.href === path))?.key ?? ''
49
50
  }
50
51
 
51
52
  /** Sidebar groups for the page's section (none outside the sections). */
52
- export function groupsOf(sections: Section[], path: string): Group[] {
53
+ export function groupsOf(sections: readonly Section[], path: string): readonly Group[] {
53
54
  return sections.find((s) => s.key === sectionKey(sections, path))?.groups ?? []
54
55
  }
55
56
 
56
57
  /** "Guide › Templates" */
57
- export function crumb(sections: Section[], path: string): string {
58
+ export function crumb(sections: readonly Section[], path: string): string {
58
59
  const section = sections.find((s) => s.key === sectionKey(sections, path))
59
60
  const group = section?.groups.find((g) => g.items.some((i) => i.href === path))
60
61
  return section && group ? `${section.title} › ${group.title}` : ''
61
62
  }
62
63
 
63
64
  /** Previous and next pages in sidebar order; an empty item (href '') when there is none. */
64
- export function pager(sections: Section[], path: string): { prev: Item; next: Item } {
65
+ export function pager(sections: readonly Section[], path: string): { prev: Item; next: Item } {
65
66
  const section = sections.find((s) => s.key === sectionKey(sections, path))
66
67
  const items = section ? flat(section) : []
67
68
  const i = items.findIndex((item) => item.href === path)
@@ -79,7 +80,7 @@ export type PageInfo = { href: string; title: string; file: string; group?: stri
79
80
  * section's title), sorted by `order`, then title. The config wins: its groups come first and its items replace pages
80
81
  * with the same href, so it can rename, reorder and add (non-page) entries. An empty section `href` means its first page.
81
82
  */
82
- export function pagesNav(sections: Section[], pages: PageInfo[]): Section[] {
83
+ export function pagesNav(sections: readonly Section[], pages: readonly PageInfo[]): Section[] {
83
84
  return sections.map((section) => {
84
85
  const groups = section.groups.map((g) => ({ title: g.title, items: [...g.items] }))
85
86
  const listed = new Set(groups.flatMap((g) => g.items.map((i) => i.href)))
@@ -2,7 +2,8 @@
2
2
  Your progress, read from this browser: XP, lessons done, quiz accuracy, badges and each lesson's state, with Reset.
3
3
  Without JavaScript (or with storage blocked) it says so; the lessons are still links.
4
4
  --}}
5
- @input lessons: { title: string; href: string }[]
5
+ @interface Lesson { title: string; href: string }
6
+ @input lessons: Lesson[]
6
7
 
7
8
  <script>
8
9
  import { load, reset, save, stats } from './progress.ts'
@@ -10,7 +11,7 @@
10
11
  let done = state(0)
11
12
  let accuracy = state('')
12
13
  let badges = state([])
13
- let statuses = state({})
14
+ let statuses = state(Object.create(null)) // lesson href → its status
14
15
  let mode = state('waiting')
15
16
  let confirming = state(false)
16
17
  function refresh() {
package/src/progress.ts CHANGED
@@ -69,7 +69,7 @@ export type Stats = {
69
69
  }
70
70
 
71
71
  /** XP (10 a step, 50 a lesson, 20 a quiz right the first time), counts and badges, for the lessons of a path. */
72
- export function stats(p: Progress, lessons: string[]): Stats {
72
+ export function stats(p: Progress, lessons: readonly string[]): Stats {
73
73
  const done = lessons.filter((l) => p.done.includes(l)).length
74
74
  const steps = lessons.reduce((n, l) => n + (p.steps[l]?.length ?? 0), 0)
75
75
  const answers = Object.values(p.quizzes)
@@ -24,7 +24,7 @@
24
24
  detail = { ok, done: true, text: ok ? 'Correct.' : 'The answer is: ' + options[answer] + '.' }
25
25
  quiz()?.setAttribute('data-answered', ok ? 'right' : 'wrong')
26
26
  const details = quiz()?.querySelector('.quiz-answer')
27
- if (details) details.open = true
27
+ if (details instanceof HTMLDetailsElement) details.open = true
28
28
  }
29
29
  quiz()?.dispatchEvent(new CustomEvent('quiz:result', { bubbles: true, detail }))
30
30
  }
package/src/search.d.ts CHANGED
@@ -10,6 +10,9 @@ export type Section = {
10
10
  */
11
11
  /** Sections per `s-<n>.json` file. */
12
12
  export declare const CHUNK = 64;
13
+ /** The site's base path ('/' unless Vite's `base` is set), where the search index is fetched from. */
14
+ /** @type {string} */
15
+ export declare const base: string;
13
16
  /** @param {string} text @returns {string[]} lowercase words of two or more letters, accents folded */
14
17
  export declare function tokens(text: string): string[];
15
18
  /** @param {string} word */
package/src/search.js CHANGED
@@ -13,6 +13,10 @@
13
13
  /** Sections per `s-<n>.json` file. */
14
14
  export const CHUNK = 64
15
15
 
16
+ /** The site's base path ('/' unless Vite's `base` is set), where the search index is fetched from. */
17
+ /** @type {string} */
18
+ export const base = /** @type {any} */ (import.meta).env?.BASE_URL ?? '/' // Vite's import.meta.env, when there is one
19
+
16
20
  /** @param {string} text @returns {string[]} lowercase words of two or more letters, accents folded */
17
21
  export function tokens(text) {
18
22
  return (text.normalize('NFKD').replace(/[̀-ͯ]/g, '').toLowerCase().match(/[a-z0-9]+/g) ?? []).filter((w) => w.length > 1)
package/src/search.tscl CHANGED
@@ -5,13 +5,12 @@
5
5
  --}}
6
6
  <script>
7
7
  import { navigate } from 'clarusjs/navigation'
8
- import { search } from './search.js'
8
+ import { base, search } from './search.js'
9
9
  let query = state('')
10
10
  let results = state([])
11
11
  let active = state(0)
12
12
  let searched = state(false)
13
13
  const status = derived(() => !searched ? '' : results.length === 0 ? 'No results for "' + query + '"' : results.length === 1 ? '1 result' : results.length + ' results')
14
- const base = import.meta.env?.BASE_URL ?? '/'
15
14
  const load = (file) => fetch(base + 'search/' + file).then((r) => (r.ok ? r.json() : {}))
16
15
  let latest = 0
17
16
  async function run(value) {
@@ -23,12 +22,12 @@
23
22
  active = 0
24
23
  searched = value.trim() !== ''
25
24
  }
26
- const dialog = () => document.querySelector('.search-dialog')
25
+ const dialog = () => { const d = document.querySelector('.search-dialog'); return d instanceof HTMLDialogElement ? d : null }
27
26
  function open() {
28
27
  const d = dialog()
29
28
  if (!d || d.open) return
30
29
  d.showModal()
31
- d.querySelector('input').select()
30
+ d.querySelector('input')?.select()
32
31
  }
33
32
  function move(by) {
34
33
  if (!results.length) return
@@ -38,13 +37,13 @@
38
37
  function key(event) {
39
38
  if (event.key === 'ArrowDown') { event.preventDefault(); move(1) }
40
39
  else if (event.key === 'ArrowUp') { event.preventDefault(); move(-1) }
41
- else if (event.key === 'Enter' && results.length) { event.preventDefault(); dialog().close(); navigate(results[active].u) }
40
+ else if (event.key === 'Enter' && results.length) { event.preventDefault(); dialog()?.close(); navigate(results[active].u) }
42
41
  // One Escape closes (a search field would only clear itself first).
43
- else if (event.key === 'Escape') { event.preventDefault(); dialog().close() }
42
+ else if (event.key === 'Escape') { event.preventDefault(); dialog()?.close() }
44
43
  }
45
44
  if (typeof document !== 'undefined') {
46
45
  document.addEventListener('keydown', (event) => {
47
- const typing = event.target.closest?.('input, textarea, select, [contenteditable]')
46
+ const typing = event.target instanceof Element && event.target.closest('input, textarea, select, [contenteditable]')
48
47
  if ((event.key === 'k' && (event.metaKey || event.ctrlKey)) || (event.key === '/' && !typing)) {
49
48
  event.preventDefault()
50
49
  open()
@@ -60,7 +59,7 @@
60
59
  <kbd aria-hidden="true">⌘K</kbd>
61
60
  </button>
62
61
  {{-- A click on the backdrop (the dialog itself, outside its box) closes it; so does choosing a result. --}}
63
- <dialog class="search-dialog" aria-label="Search the docs" (click)="($event.target.matches('dialog') || $event.target.closest('a')) && dialog().close()">
62
+ <dialog class="search-dialog" aria-label="Search the docs" (click)="($event.target.matches('dialog') || $event.target.closest('a')) && dialog()?.close()">
64
63
  <div class="search-box">
65
64
  <input type="search" role="combobox" aria-label="Search the docs" aria-controls="search-results" aria-autocomplete="list" [aria-expanded]="results.length ? 'true' : 'false'" [aria-activedescendant]="results.length ? 'search-result-' + active : false" placeholder="Search the docs" autocomplete="off" spellcheck="false" (input)="run($event.target.value)" (keydown)="key($event)">
66
65
  <p class="search-status" role="status">{{ status }}</p>