Markdown, code and notebooks
Show Markdown, source code in 200+ languages, plain text, huge logs and Jupyter notebooks in React, with highlighting, search and printing.
Markdown
Rendered with markdown-it, with raw HTML off and the output sanitised by DOMPurify. Tables, task lists and highlighted code blocks are supported, and headings form the outline.
Code and text
Highlighted by Shiki with its JavaScript regex engine, loading only the grammar the file needs. The language comes from the file name, and from a shebang line when there is no extension. Colours meet WCAG AA contrast in light and dark themes.
Long files
Plain text files over 3,000 lines, such as logs and dumps, are windowed by blocks of lines. Line numbers, search and printing still cover the whole file.
Jupyter notebooks
Our own nbformat 3 and 4 reader shows Markdown cells (with attachments), highlighted code with In and Out prompts, and saved outputs: sanitised HTML tables, PNG, JPEG and SVG plots, text, and ANSI-coloured tracebacks. Headings form the outline. Nothing is executed.
Controller
| State | For code, text, Markdown and notebooks |
|---|---|
page, pageCount | No pages: pageCount is 0. |
zoom, scale | Number zooms make the text larger or smaller; fit modes do not apply. |
outline | Markdown and notebooks: their headings, nested. Code and plain text: none. |
thumbnails | None. |
search.count | Matches in the whole file, including lines not drawn yet in long logs. matchCase and wholeWord are supported. |
can | zoom, search, print, download; sidebar for Markdown and notebooks with headings. |
Headless
A text viewer with search options and a table of contents that appears only when the file has headings:
'use client';
// Markdown, code and notebooks: search with match-case and whole-word options, and a table of
// contents when the file has headings (Markdown, notebooks). Code files have no outline.
import { useState } from 'react';
import { DocumentSurface, useDocViewer, useDocumentSearch, useOutline, type DocSource } from '@anypreview/react';
import '@anypreview/react/styles.css';
export default function TextViewer({ source }: { source: DocSource }) {
const viewer = useDocViewer({ source });
const toc = useOutline(viewer);
const [query, setQuery] = useState('');
const [matchCase, setMatchCase] = useState(false);
const [wholeWord, setWholeWord] = useState(false);
const results = useDocumentSearch(viewer, query, { matchCase, wholeWord });
return (
<div className="flex h-full text-sm">
{toc.length > 0 && (
<nav className="w-48 shrink-0 overflow-y-auto border-r p-3">
{toc.map((h, i) => (
<button key={i} className="block text-left" onClick={() => viewer.goToOutline(h)}>
{h.title}
</button>
))}
</nav>
)}
<main className="flex min-w-0 flex-1 flex-col">
<header className="flex flex-wrap items-center gap-3 border-b p-2">
<input className="rounded border px-2" placeholder="Search" value={query} onChange={(e) => setQuery(e.target.value)} />
<label><input type="checkbox" checked={matchCase} onChange={(e) => setMatchCase(e.target.checked)} /> Aa</label>
<label><input type="checkbox" checked={wholeWord} onChange={(e) => setWholeWord(e.target.checked)} /> Whole word</label>
{query && <span>{results.count} matches</span>}
<button className="ml-auto" onClick={viewer.zoomIn}>Larger text</button>
<button onClick={viewer.zoomOut}>Smaller text</button>
<button onClick={() => void viewer.print()}>Print</button>
</header>
<DocumentSurface viewer={viewer} className="min-h-0 flex-1" />
</main>
</div>
);
}