Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions _extensions/fontawesome/fontawesome.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
34 changes: 28 additions & 6 deletions example.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ 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 `<icon-name>`:
``` markdown
{{{< fa <icon-name> >}}}
```

- Optional `<group>`, `<size=...>`, and `<title=...>`:
- Optional `<group>`, `<size=...>`, `<color=...>`, `<title=...>`, and `<label=...>`:
``` markdown
{{{< fa <group> <icon-name> <size=...> <title=...> >}}}
{{{< fa <group> <icon-name> <size=...> <color=...> <title=...> <label=...> >}}}
```

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 |
Expand All @@ -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!