viewer

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 33 Imported by: 0

Documentation

Overview

Code generated from @primer/octicons 19.15.1 (MIT). DO NOT EDIT by hand.

Package viewer renders blob content for the file viewer page. Each file type is one Renderer behind a small interface; the server asks the Registry and never learns what file types exist. Structural blob states (symlink, LFS pointer, too big, empty) stay in the server: they are states of the record, not content (spec 2011 doc 18).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChromaCSS

func ChromaCSS() string

ChromaCSS renders the github light style plus the github-dark style scoped to the dark color mode, with an auto-mode media query copy.

func FindReadme

func FindReadme(entries []backend.TreeEntry) (string, bool)

FindReadme picks the readme github would show for a directory listing.

func HighlightLines

func HighlightLines(filename, content string) []template.HTML

HighlightLines tokenizes content and returns one HTML fragment per line, using chroma class names so the github and github-dark styles switch with the page theme instead of re-rendering.

func IsMarkdownFile

func IsMarkdownFile(name string) bool

IsMarkdownFile reports whether the blob should get a rendered preview.

func MIME

func MIME(ext string) (string, bool)

MIME returns the Content-Type for a lowercased extension with dot.

func Octicon

func Octicon(name string) template.HTML

Octicon returns the inline SVG for a named icon, "" markup for unknown names. Page templates use this through the server's octicon func; the markdown pipeline uses the map directly.

func RenderMarkdown

func RenderMarkdown(mctx MarkdownContext, src []byte) template.HTML

RenderMarkdown runs the pipeline: goldmark with GFM and our transformers, then bluemonday, then trusted post-passes that add heading anchors and alert icons. Sanitizing after rendering is the security boundary; the post-passes only touch markup the sanitizer already approved.

Types

type Input

type Input struct {
	Name     string // base name, "model.parquet"
	Path     string // repo-relative path, "data/model.parquet"
	Ext      string // lowercased extension with dot, ".parquet"
	Content  []byte
	Size     int64  // true blob size, equals len(Content) here
	Binary   bool   // backend's NUL-byte verdict
	RawURL   string // "/{key}/raw/{ref}/{path}", for media and frame sources
	RepoPath string // "/{owner}/{repo}", for markdown link rewriting
	Ref      string // resolved display ref, same purpose
	Plain    bool   // ?plain=1: the user asked for source, not preview
}

Input is everything a renderer may look at. Content is the full blob, already capped at backend.MaxBlobBytes by the backend; TooBig blobs never reach the registry.

type MarkdownContext

type MarkdownContext struct {
	RepoPath string // "/owner/name"
	Ref      string
	Dir      string // directory of the source file, "" at the root
}

MarkdownContext tells the rewriter where the markdown file lives so relative links and images resolve to blob and raw URLs.

type Output

type Output struct {
	Kind   string        // stable id: "code", "csv", "video", ...
	Body   template.HTML // the content area, safe by construction
	Info   string        // toolbar text: "183 lines", "100 rows x 12 columns"
	Toggle bool          // offer the Preview / Code segmented control
	Note   string        // cap warning shown above the body, "" for none
}

Output is a rendered fragment plus toolbar facts.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry is an ordered list of renderers; the first Match wins.

func NewRegistry

func NewRegistry(rs ...Renderer) *Registry

func (*Registry) Probe

func (g *Registry) Probe(in Input) (string, bool)

Probe reports the kind that would claim the input, without rendering. The server probes with Plain unset to keep the Preview / Code toggle visible while the plain view is showing.

func (*Registry) Render

func (g *Registry) Render(in Input) (Output, bool)

Render walks renderers in order. A renderer error is a degradation, never a failure: the walk continues, so a corrupt container falls through to whatever would have claimed the file otherwise. ok reports whether anything rendered; the server shows the structural binary or empty notice when nothing did.

type Renderer

type Renderer interface {
	Kind() string
	Match(in Input) bool
	Render(in Input) (Output, error)
}

Renderer is one viewer. Match must be cheap (extension checks, magic bytes); Render may parse. A Renderer that matched but cannot render returns an error and the registry falls through to the next match.

func Audio

func Audio() Renderer

Audio renders an <audio> element over the raw URL. preload="metadata" keeps a directory crawl from pulling whole files; never autoplay. Codec support is the browser's problem: the element shows its native error UI when it cannot play the file.

func Code

func Code() Renderer

Code is the final text fallback: the chroma table with line ids that back the L{n} anchor contract (doc 08).

func Docx

func Docx() Renderer

Docx extracts readable text from a docx file: headings, paragraphs, bold and italic runs, flat lists, one level of tables, external hyperlinks. Layout fidelity is a non-goal; the bar is strictly better than a binary notice, which is what github.com shows. Every text node passes through template escaping and the only markup in the output is the fixed mapping below, so nothing unsanitized can reach the page (spec 2011 doc 18 section 12).

func Image

func Image() Renderer

Image renders raster and vector images through an <img> over the raw URL. SVG stays behind <img>, where scripts never execute; the raw response behind it keeps its sandbox CSP for direct navigation.

func Markdown

func Markdown() Renderer

Markdown is the doc 09 pipeline behind the registry: rendered preview by default, deferring to the code viewer under ?plain=1.

func PDF

func PDF() Renderer

PDF embeds the browser's own viewer in an iframe over the raw URL. No magic check: a text file named x.pdf shows the viewer's broken document message, which is more honest than code-viewing it. The iframe carries no sandbox attribute on purpose: a sandboxed frame blocks Chromium's PDF plugin outright, and the raw response behind it already drops the CSP sandbox for application/pdf with the frame-ancestors 'self' analysis from spec 2011 doc 18 section 4.3.

func Parquet

func Parquet() Renderer

Parquet renders the schema panel and the first rows of a parquet file. The schema comes first because for large datasets the schema is the document; the row count in the Note is free footer metadata, never a full scan. A corrupt or truncated file falls through to the structural binary notice per the registry's error semantics.

func Tabular

func Tabular() Renderer

Tabular renders csv and tsv files as a table, github.com style: the first record is always the header, rows get a number gutter, and the Code view stays one toggle away. A file whose first record does not parse even lazily falls through to the code viewer.

func Video

func Video() Renderer

Video renders a <video> element over the raw URL. Same rules as audio: metadata preload, no autoplay, no poster. CSS caps the element at the blob box width and 70vh.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL