# hungyuantu/goldilocks architecture

> Architecture diagram of the GitHub repository hungyuantu/goldilocks, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hungyuantu/goldilocks
- Repository: https://github.com/hungyuantu/goldilocks

## Overview

Goldilocks is a browser-side custom element that typesets English paragraph text with justified line breaks and hyphenation. A user places text in `<goldi-break lang="en">`; the element validates its input and dimensions, parses text into breakable nodes, measures candidate fragments, chooses line breaks, and replaces its content with marked-up text that permits browser wrapping. The graph also shows the optional debug dump. Relationships follow the sampled calls and README; unsampled measurement details are not inferred.

Main entry points: [`src/main.ts`](https://github.com/hungyuantu/goldilocks/blob/main/src/main.ts) (Custom element), [`src/nodes/defs.ts`](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/defs.ts) (Breakable nodes), [`src/nodes/from.ts`](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/from.ts) (Text parser), [`src/nodes/wrap.ts`](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/wrap.ts) (Line breaker)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Browser runtime"]
  node_bootstrap["Package entry<br/>[init.ts]"]
  node_element["Custom element<br/>[main.ts]"]
  node_dom["Rendered paragraph"]
end

subgraph group_layout["Paragraph layout"]
  node_parser["Text parser<br/>[from.ts]"]
  node_node_model["Breakable nodes<br/>[defs.ts]"]
  node_measurement["Text measurement<br/>[size.ts]"]
  node_line_breaker["Line breaker<br/>[wrap.ts]"]
  node_knuth["Knuth parameters<br/>[defs.ts]"]
  node_marker["Break marking<br/>[mark.ts]"]
end

subgraph group_hyphenation["English hyphenation"]
  node_hyphenator["Word hyphenation<br/>[hyph.ts]"]
  node_patterns["English patterns<br/>[trie.ts]"]
end

subgraph group_debug["Debugging"]
  node_debug_hook["Debug hook<br/>[init.ts]"]
  node_debug_dump["Break dump<br/>[dump.ts]"]
end

node_user(("Page author"))

node_user -->|"adds text"| node_element
node_bootstrap -->|"loads"| node_element
node_bootstrap -->|"loads"| node_debug_hook
node_element -->|"parses text"| node_parser
node_element -->|"measures nodes"| node_measurement
node_element -->|"chooses breaks"| node_line_breaker
node_element -->|"marks breaks"| node_marker
node_element -->|"updates content"| node_dom
node_parser -->|"builds nodes"| node_node_model
node_parser -->|"gets breakpoints"| node_hyphenator
node_line_breaker -->|"uses nodes"| node_node_model
node_line_breaker -->|"scores candidates"| node_knuth
node_marker -->|"clips text"| node_node_model
node_hyphenator -->|"looks up patterns"| node_patterns
node_debug_hook -->|"dumps breaks"| node_debug_dump
node_debug_dump -->|"formats break data"| node_node_model

click node_bootstrap "https://github.com/hungyuantu/goldilocks/blob/main/src/init.ts"
click node_element "https://github.com/hungyuantu/goldilocks/blob/main/src/main.ts"
click node_parser "https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/from.ts"
click node_node_model "https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/defs.ts"
click node_measurement "https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/size.ts"
click node_line_breaker "https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/wrap.ts"
click node_knuth "https://github.com/hungyuantu/goldilocks/blob/main/src/knuth/defs.ts"
click node_marker "https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/mark.ts"
click node_hyphenator "https://github.com/hungyuantu/goldilocks/blob/main/src/utils/hyph.ts"
click node_patterns "https://github.com/hungyuantu/goldilocks/blob/main/src/utils/trie.ts"
click node_debug_hook "https://github.com/hungyuantu/goldilocks/blob/main/src/debug/init.ts"
click node_debug_dump "https://github.com/hungyuantu/goldilocks/blob/main/src/debug/dump.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bootstrap,node_element,node_dom toneBlue
class node_parser,node_node_model,node_measurement,node_line_breaker,node_knuth,node_marker toneAmber
class node_hyphenator,node_patterns toneMint
class node_debug_hook,node_debug_dump toneRose
class node_user toneIndigo
```

## Components

### Browser runtime

- [Package entry](https://github.com/hungyuantu/goldilocks/blob/main/src/init.ts): `src/init.ts`
- [Custom element](https://github.com/hungyuantu/goldilocks/blob/main/src/main.ts): `src/main.ts`
- **Rendered paragraph**

### Paragraph layout

- [Text parser](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/from.ts): `src/nodes/from.ts`
- [Breakable nodes](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/defs.ts): `src/nodes/defs.ts`
- [Text measurement](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/size.ts): `src/nodes/size.ts`
- [Line breaker](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/wrap.ts): `src/nodes/wrap.ts`
- [Knuth parameters](https://github.com/hungyuantu/goldilocks/blob/main/src/knuth/defs.ts): `src/knuth/defs.ts`
- [Break marking](https://github.com/hungyuantu/goldilocks/blob/main/src/nodes/mark.ts): `src/nodes/mark.ts`

### English hyphenation

- [Word hyphenation](https://github.com/hungyuantu/goldilocks/blob/main/src/utils/hyph.ts): `src/utils/hyph.ts`
- [English patterns](https://github.com/hungyuantu/goldilocks/blob/main/src/utils/trie.ts): `src/utils/trie.ts`

### Debugging

- [Debug hook](https://github.com/hungyuantu/goldilocks/blob/main/src/debug/init.ts): `src/debug/init.ts`
- [Break dump](https://github.com/hungyuantu/goldilocks/blob/main/src/debug/dump.ts): `src/debug/dump.ts`

### Other components

- **Page author**

## Connections

- Page author → Custom element: adds text (evidence: `README.md`)
- Package entry → Custom element: loads (evidence: `src/init.ts`)
- Package entry → Debug hook: loads (evidence: `src/init.ts`)
- Custom element → Text parser: parses text (evidence: `src/main.ts`)
- Custom element → Text measurement: measures nodes (evidence: `src/main.ts`)
- Custom element → Line breaker: chooses breaks (evidence: `src/main.ts`)
- Custom element → Break marking: marks breaks (evidence: `src/main.ts`)
- Custom element → Rendered paragraph: updates content (evidence: `src/main.ts`)
- Text parser → Breakable nodes: builds nodes (evidence: `src/nodes/from.ts`)
- Text parser → Word hyphenation: gets breakpoints (evidence: `src/nodes/from.ts`)
- Line breaker → Breakable nodes: uses nodes (evidence: `src/nodes/wrap.ts`)
- Line breaker → Knuth parameters: scores candidates (evidence: `src/nodes/wrap.ts`)
- Break marking → Breakable nodes: clips text (evidence: `src/nodes/mark.ts`)
- Word hyphenation → English patterns: looks up patterns (evidence: `src/utils/hyph.ts`)
- Debug hook → Break dump: dumps breaks (evidence: `src/debug/init.ts`)
- Break dump → Breakable nodes: formats break data (evidence: `src/debug/dump.ts`)
