Pretext Justify

approved

by MYQ

Optimal Knuth-Plass text justification for reading view. - This plugin has not been manually reviewed by Obsidian staff.

1 stars271 downloadsUpdated 1mo agoMIT

Pretext Justify

Knuth-Plass optimal line-breaking for the Obsidian reading view. Replaces the browser's default word-wrap with typographically optimal paragraph layout.

Features

FeatureDescription
Optimal line breakingShortest-path DP over feasible break candidates; minimizes accumulated paragraph badness
HyphenationOptional soft-hyphen insertion for more even spacing
Last-line handlingLast line left-aligned; overflow prevention via DP penalty
Resize responsivenessAuto re-justifies on width change, throttled via rAF (2 paragraphs per frame)
Inline formattingPreserves bold, italic, links, code — anything the reading view renders within a paragraph
Fully offlineAll processing happens locally — no network requests, no telemetry, no analytics

How it works

The plugin uses a dynamic programming algorithm developed by Donald Knuth and Michael Plass for TeX. It enumerates all feasible break positions (word boundaries and soft hyphens), builds a graph where each edge is weighted by a "badness" cost, and finds the shortest path through:

dp[j] = min over i<j of dp[i] + badness(i, j)

The badness function combines:

  • Cubic stretch/shrink ratio — the core Knuth cost, proportional to |ratio|³
  • River penalty — heavy cost when inter-word spacing exceeds 150% of normal, preventing visual "rivers" of white space
  • Tight penalty — cost when spacing drops below the configured threshold, avoiding cramped lines
  • Hyphen penalty — small constant that prefers natural word breaks over hyphenated ones

For the last line, the algorithm penalizes break choices whose natural width (word text + normal spaces) exceeds the container, favoring solutions where the last line fits without compression.

The plugin uses Pretext for text measurement via Canvas API, and preserves DOM structure by extracting character ranges from a clone of the original paragraph — inline formatting survives the transformation.

Third-party code

This plugin bundles @chenglou/pretext (MIT) for Canvas-based text measurement. See the LICENSE file for the full MIT terms.

Configuration

SettingDefaultDescription
HyphenationOnInsert soft hyphens for more even spacing
Minimum spacing ratio0.50Lowest allowed word spacing as a fraction of normal space (0.30–0.90)
Tight penalty threshold0.75Fraction of normal space below which the algorithm penalizes tight lines (0.50–1.00)
Text cache size200Number of paragraphs cached to avoid remeasurement on resize (50–1000)

Compliance

  • No network access — all processing is local to the device. No data is sent or received.
  • No telemetry — no analytics, no crash reporting, no usage tracking of any kind.
  • No ads — the plugin displays no advertisements.
  • No self-updating — updates are handled exclusively through Obsidian's built-in plugin update mechanism.

Development

git clone https://github.com/TYBLHQY/obsidian-pretext
cd obsidian-pretext
npm install
npm run build

Copy main.js, manifest.json, and styles.css to your vault's .obsidian/plugins/pretext-justify/.

License

MIT

For plugin developers

Search results and similarity scores are powered by semantic analysis of your plugin's README. If your plugin isn't appearing for searches you'd expect, try updating your README to clearly describe your plugin's purpose, features, and use cases.