8000 GitHub - tshort/typ-pad: Create online apps using Typst · GitHub
[go: up one dir, main page]

Skip to content
tshortPublic

About

Create online apps using Typst

Resources

Stars

12 stars

Watchers

1 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

typ-pad

Create online apps using Typst

typ-pad provides a Typst template to provide an easy way to write one-file, one-page web apps. Use Typst to generate the page content, including input elements. Then use JavaScript and Typst (optionally) to calculate and display results. The standalone file can be used by itself, either through a webserver or even just opening the HTML file in a browser.

By default, typ-pad apps use mdpad (GitHub). mdpad provides user-interface code to make it easier to write apps. Originally, this was used with Markdown, but it also works well with pages generated by Typst. It works well with traditional static-site generators like Jekyll or Hugo. I've used mdpad professionally for several engineering apps (example). mdpad is easy, because the user input is very basic. By default, results are updated any time users change inputs. For more sophisticated interactivity and state management, one can use more sophisticated JavaScript packages.

For generating HTML with Typst, the hyperscript package helps.

Examples

  • Example 1: Basic Typst content generation with JavaScript calculations.
  • Example 2: Typst is used to generate content on the fly.

Template

This package provides the typ-pad function. Use it with #show. Here is a snippet from one of the examples:

#show: typ-pad.with(
  title: "Typ-pad: summing sinusoids",
  links: (
    (rel: "stylesheet", href: "https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"),
    (rel: "icon", type: "svg/xml", href: "data:image/svg+xml,<svg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'><rect fill='none' height='256' width='256'/><path d='M24,128c104-224,104,224,208,0' fill='none' stroke='%23000' stroke-linecap='round' stroke-linejoin='round' stroke-width='19'/></svg>"),
  ),
  typst-online: typs, 
  js: js)

The typ-pad function has several named arguments that affect HTML generation.

  • title (default: ""): Title of the HTML document.
  • heads (default: ()): Array of content to be included in <head>.
  • heads-default (default: true): Whether to include the following defaults in <head>.
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  • links (default: ()): Array of dictionaries where each includes parameters of a <link> to include in <head>.
  • js (default: ""): Code to include at the end of the body. Can be a string or raw.
  • js-includes (default: ()): An array of JavaScript links to include.
  • mdpad (default: true): Include JavaScript mdpad files.
  • typst-online (default: (:)): Include JavaScript and WebAssembly code to enable Typst to be used to generate content online.
  • full-html (default: true): Include <html>, <head>, and <body> tags.

To use mdpad functionality, define one or both of the functions mdpad_init and mdpad_update i the js block passed to typ-pad. In JavaScript, the variable mdpad holds the results of form inputs that have the mdpad attribute.

To enable Typst online, use the typst-online argument to typ-pad. This is a dictionary mapping id's to Typst code. Upon updates to inputs, the Typst code is compiled, and the result is placed on the page within at the given id. Within Typst code, the mdpad dictionary is also available with the form inputs.

Comparisons

Advantages of using Typst to generate online apps include:

  • Only a single input file is required, and Typst generates a single output file. It should fit in well with static-site generators.
  • Pages work even without a webserver.
  • Typst is more powerful than Markdown. Functions to generate content are natural.
  • Typst or JavaScript code is easy to include via triple backticks, and these are syntax highlighted in VS Code.
  • Typst can optionally be used online to generate content dynamically.

Some disadvantages include:

  • On cannot currently assign id's to components of an SVG file that Typst generates. That makes it difficult to provide interaction from the outside.
  • The HTML export of Typst is still rather basic.

The online Typst capability is restrictive:

  • Currently, only SVG's can be inserted by the online Typst capability. In the future, typst-ts might be expanded to use the HTML exporter to allow a larger variety of content generation.
  • Online Typst usage requires downloading a 20-MB WebAssembly file.
  • Unlike JavaScript, online Typst code can't interact with page content.

About

Create online apps using Typst

Resources

Stars

12 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

0