Skip to the page
Social CrawlDocs

The render package

Updated
View as Markdown

What packages/render does, every function it exports, how a post becomes an offline page, text and Markdown, and the rules that keep saved pages safe.

On this page

@social-crawl/render turns a Post into things you can read: the offline index.html, plain text, Markdown, the video and playlist blocks, the repository sidebar, and layouts. Its only runtime dependency is @social-crawl/schema (no third-party package), and it has no network or file access, so the same code runs on the server (saved bundles, shared pages, ZIPs) and in tests.

Build it with yarn workspace @social-crawl/render build. Test it with yarn workspace @social-crawl/render test.

Pure functions#

Every function takes data and returns text. Nothing here reads a file or the network, and no function uses the clock or a random number, so the same post always gives the same page. That is why the page of a post can be redrawn later from post.json.

What it exports#

ExportWhat it does
renderPostHtml(post, options)The offline page. options.lang sets the language, options.remoteMedia shows media from its remote address for a preview (never for saved bundles) and allows https: images in the CSP, options.layout is a layout template for an article.
renderPostText(post)The plain text, saved as text/post.txt.
renderPostMarkdown(post, options)The Markdown. links: "bundle" (default) points at saved files such as images/0.jpg, and links: "remote" at the original addresses. baseUrl puts an address in front of every saved file, so the text works away from the folder (what a share link's Markdown uses).
transcriptSection(...)The words of a video as a Markdown section.
renderPlaylistInfo, renderPlaylistMarkdown, playlistText, PLAYLIST_CSSA saved YouTube playlist.
renderRepoInfo, renderRepoMarkdown, repoZip, shortCountA GitHub repository's sidebar and Markdown.
renderVideoHtml, clock, SEEK_SCRIPT, SEEK_SCRIPT_HASHThe video block, and the small script that seeks a video from the time links, with its CSP hash.
parseLayout, renderLayout, pickLayout, layoutDomain, LAYOUT_CSS, LAYOUT_SOURCE_MAX, STARTER_LAYOUTSThe layout language. See The layout language.
renderSourceBadge, renderAvatarFallback, SOURCE_BADGE_CSS, sourceInfo, sourceInfoOf, sourceKind, PLATFORM_LOGOS, SITE_LOGOS, siteLogoForThe source badge, the logos and the fallback avatar.
THEMESPer-platform accent colour, card width and font stack.
linksIn, textParts, URL_PATTERNFinds links in plain text.
escapeHtml, safeHttpUrl, safeLinkUrl, safeBundlePathThe escaping and URL checks everything else uses.
mediaPath, extensionForMime, MEDIA_FOLDERS, BUNDLE_PATHSWhere a file goes inside a bundle (images/0.jpg, videos/0.mp4) and the fixed names index.html, post.json, text/post.txt and post.md.
fileLabel, formatBytesWords for a file and its size.
articleBlocks, imageKey, isAuthorPictureHelpers for article pictures.

Safety rules for the page#

The offline page is a security boundary, because it shows text from the whole internet.

  • No scripts. A post or article page has a Content-Security-Policy of default-src 'none' with script-src 'none', and allows only its own images and media (and inline styles). The one exception is the video page, whose small seek script for time links is allowed by its hash (SEEK_SCRIPT_HASH), never by unsafe-inline.
  • Everything is escaped. Post text, names and titles go through escapeHtml.
  • Links are checked. safeLinkUrl keeps http(s), ftp(s), sftp, magnet, mailto and tel, and drops everything else (javascript:, data:, file:).
  • Paths are checked. safeBundlePath accepts only paths inside the bundle.
  • Article blocks are data. An article is blocks, never HTML, so nothing in a saved article can inject markup.

Tests#

render.test.ts and layout.test.ts use Vitest. They render posts of every platform and check the output for what must be there and what must never be. Add a case when you change what a page draws.

Add a new kind of page#

  1. Extend the schema in packages/schema.
  2. Write a render<Thing> function that returns HTML (and one for Markdown).
  3. Call it from renderPostHtml, and add its CSS to the page's inline style so the page stays self-contained.
  4. Add tests, then rebuild with yarn build:libs.