Skip to Content
AnyPreview

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.

Live: README.md rendered by the library in this page.Open in the playground

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.

Live: app.ts rendered by the library in this page.Open in the playground

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.

Live: a 100,000-line log.Open in the playground

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.

Live: analysis.ipynb rendered by the library in this page.Open in the playground

Controller

StateFor code, text, Markdown and notebooks
page, pageCountNo pages: pageCount is 0.
zoom, scaleNumber zooms make the text larger or smaller; fit modes do not apply.
outlineMarkdown and notebooks: their headings, nested. Code and plain text: none.
thumbnailsNone.
search.countMatches in the whole file, including lines not drawn yet in long logs. matchCase and wholeWord are supported.
canzoom, search, print, download; sidebar for Markdown and notebooks with headings.
Live: useDocViewer on README.md. Press a command and watch the state change.

Headless

A text viewer with search options and a table of contents that appears only when the file has headings:

examples/headless/text-and-code.tsx
'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>
  );
}
Live: the example above, built only with the public hooks, running on README.md.

On this page