diff --git a/_extensions/fontawesome/fontawesome.lua b/_extensions/fontawesome/fontawesome.lua index ff64dca..cf6af4b 100644 --- a/_extensions/fontawesome/fontawesome.lua +++ b/_extensions/fontawesome/fontawesome.lua @@ -10,6 +10,14 @@ local function ensureHtmlDeps() }) end +local function ensure_typst_font_awesome() + if included_font_awesome then + return + end + included_font_awesome = true + quarto.doc.include_text("in-header", "#import \"@preview/fontawesome:0.1.0\": *") +end + local function isEmpty(s) return s == nil or s == '' end @@ -35,6 +43,53 @@ local function isValidSize(size) return "" end +local function convert_fa_relative_size(size) + local validSizes = { + "2xs", + "xs", + "sm", + "lg", + "xl", + "2xl", + "tiny", + "scriptsize", + "footnotesize", + "small", + "normalsize", + "large", + "Large", + "LARGE", + "huge", + "Huge" + } + + local relativeSizes = { + "0.625em", + "0.75em", + "0.875em", + "1.25em", + "1.5em", + "2em", + "0.125em", + "0.5em", + "0.625em", + "0.75em", + "0.875em", + "1.25em", + "1.5em", + "2em", + "2.5em", + "3em" + } + + for i, v in ipairs(validSizes) do + if v == size then + return relativeSizes[i] + end + end + return size +end + return { ["fa"] = function(args, kwargs) @@ -77,6 +132,28 @@ return { else return pandoc.RawInline('tex', "{\\" .. size .. "\\faIcon{" .. icon .. "}}") end + -- detect typst + elseif quarto.doc.is_format("typst") then + ensure_typst_font_awesome() + + local color = pandoc.utils.stringify(kwargs["color"]) + if not isEmpty(size) then + size = convert_fa_relative_size(size) + size = "size: " .. size + end + + if not isEmpty(color) then + color = "fill: " .. color + + if not isEmpty(size) then + size = size .. ", " + end + end + + return pandoc.RawInline( + 'typst', + "#fa-" .. icon .. "(" .. size .. color .. ")" + ) else return pandoc.Null() end diff --git a/example.qmd b/example.qmd index e03a0f2..dac14ec 100644 --- a/example.qmd +++ b/example.qmd @@ -3,9 +3,11 @@ title: Font Awesome Quarto Extension format: html: default pdf: default + typst: default --- -This extension allows you to use font-awesome icons in your Quarto HTML and PDF documents. +This extension allows you to use font-awesome icons in your Quarto HTML, PDF LaTeX, and Typst documents. + It provides an `{{{< fa >}}}` shortcode: - Mandatory ``: @@ -13,11 +15,13 @@ It provides an `{{{< fa >}}}` shortcode: {{{< fa >}}} ``` -- Optional ``, ``, and ``: +- Optional ``, ``, ``, ``, and ``: ``` markdown - {{{< fa >}}} + {{{< fa >}}} ``` +The `title` and `label` parameters are only supported for the `html` format. The `color` parameter is only supported for the `typst` format. The `typst` format also supports brand icons without any requirement to specify a `group` parameter. + For example: | Shortcode | Icon | @@ -26,13 +30,31 @@ For example: | `{{{< fa folder >}}}` | {{< fa folder >}} | | `{{{< fa chess-pawn >}}}` | {{< fa chess-pawn >}} | | `{{{< fa brands bluetooth >}}}` | {{< fa brands bluetooth >}} | -| `{{{< fa brands twitter size=2xl >}}}` (HTML only) | {{< fa brands twitter size=2xl >}} | +| `{{{< fa battery-half size=Huge >}}}` | {{< fa battery-half size=Huge >}} | + +::: {.content-visible when-format="html"} +HTML and Typst format-specific examples: + +| Shortcode | Icon | +| -------------------------------------------------- | ----------------------------------------- | +| `{{{< fa brands twitter size=2xl >}}}` (HTML and Typst only) | {{< fa brands twitter size=2xl >}} | | `{{{< fa brands github size=5x >}}}` (HTML only) | {{< fa brands github size=5x >}} | -| `{{{< fa battery-half size=Huge >}}}` | {{< fa battery-half size=Huge >}} | -| `{{{< fa envelope title="An envelope" >}}}` | {{< fa envelope title="An envelope" >}} | +| `{{{< fa envelope title="An envelope" >}}}` (HTML only) | {{< fa envelope title="An envelope" >}} | +::: + +::: {.content-visible when-format="typst"} +HTML and Typst format-specific examples: + +| Shortcode | Icon | +| -------------------------------------------------- | ----------------------------------------- | +| `{{{< fa brands twitter size=2xl >}}}` (HTML and Typst only) | {{< fa brands twitter size=2xl >}} | +| `{{{< fa pizza-slice size=2xl color="red">}}}` (Typst only) | {{< fa pizza-slice size=2xl color="red">}} | + +::: Note that the icon sets are currently not perfectly interchangeable across formats: - `html` uses FontAwesome 6.4.2 - `pdf` uses the `fontawesome5` package, based on [FontAwesome 5](https://ctan.org/pkg/fontawesome5). +- `typst` uses the `typst-fontawesome` package - Other formats are currently not supported, but PRs are always welcome! \ No newline at end of file