Skip to main content

A Markdown extension to compile typst code blocks to SVG.

Project description

python-markdown-typst

Build and Release

A Python Markdown extension that compiles typst fenced code blocks into inline SVG images.

Features

  • Detects fenced code blocks labeled as typst with typst-preview in the first line.
  • Compiles the Typst code into SVG format using the typst compiler.

Installation

pip install markdown-typst

Usage

import markdown
from markdown_typst.typst_extension import TypstExtension

md = markdown.Markdown(extensions=[TypstExtension()])
input_text = '''

# This is a markdown title

```typst
// typst-preview
// This is a simple Typst document
= This is a typst title
```

'''

html = md.convert(input_text)
with open('output.html', 'w') as f:
    f.write(html)

It is recommended to use the following Typst code to set the page size and margins:

#set page(width: auto, height: auto, margin: .5cm)

Integrate Typst Rendering with MkDocs

To enable Typst syntax rendering in MkDocs, add the following lines to your mkdocs.yml:

markdown_extensions:
  - typst

Example Usage

# Hello, typst

!!! note  
    !!! warning
        ```typst
        // typst-preview
        // Code from https://raw.githubusercontent.com/typst/packages/main/packages/preview/cetz/0.3.2/gallery/waves.typ
        #import "@preview/cetz:0.3.2": canvas, draw, vector, matrix
        
        #set page(width: auto, height: auto, margin: .5cm)
        
        #canvas({
          import draw: *
        
          ortho(y: -30deg, x: 30deg, {
            on-xz({
              grid((0,-2), (8,2), stroke: gray + .5pt)
            })
        
            // Draw a sine wave on the xy plane
            let wave(amplitude: 1, fill: none, phases: 2, scale: 8, samples: 100) = {
              line(..(for x in range(0, samples + 1) {
                let x = x / samples
                let p = (2 * phases * calc.pi) * x
                ((x * scale, calc.sin(p) * amplitude),)
              }), fill: fill)
        
              let subdivs = 8
              for phase in range(0, phases) {
                let x = phase / phases
                for div in range(1, subdivs + 1) {
                  let p = 2 * calc.pi * (div / subdivs)
                  let y = calc.sin(p) * amplitude
                  let x = x * scale + div / subdivs * scale / phases
                  line((x, 0), (x, y), stroke: rgb(0, 0, 0, 150) + .5pt)
                }
              }
            }
        
            on-xy({
              wave(amplitude: 1.6, fill: rgb(0, 0, 255, 50))
            })
            on-xz({
              wave(amplitude: 1, fill: rgb(255, 0, 0, 50))
            })
          })
        })
        ```

snapshot1

Requirements

  • markdown
  • typst

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_typst-0.1.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_typst-0.1.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file markdown_typst-0.1.3.tar.gz.

File metadata

  • Download URL: markdown_typst-0.1.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for markdown_typst-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7334e7930e41f0cbfb0448778e6aa6453966a71825e3755c6dc03f4773f532ca
MD5 e3b229f176a3055710bb69778dfcb06c
BLAKE2b-256 417bed852ca0a0a7de6423496383a5889b747e43e9a5a5d748e42e4ff1f79952

See more details on using hashes here.

File details

Details for the file markdown_typst-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: markdown_typst-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for markdown_typst-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 def6617cd61b922182679b8fe098a24ef04327e988694b16eb2031f9cfc9c12b
MD5 7ae344b0459c8ee1d1e259e97525b1ff
BLAKE2b-256 dedd961d1f792e07281718671743e4e5fd84469a58d0f04f97d57bb997f546e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page