Skip to content

Commit

Permalink
more bug fixes + add release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
fenjalien committed Mar 23, 2024
1 parent ca6b411 commit 9037a33
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
branches:
- main

env:
MDBOOK_OUTPUT__HTML__SITE_URL: /mdbook-typst-doc/main/

jobs:
deploy:
runs-on: ubuntu-latest
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Release
on:
push:
tags:
- "*.*.*"

permissions:
contents: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo build --release
- env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tag="${GITHUB_REF#refs/tags/}"
gh release create "$tag" \
--title="$tag" \
target/release/mdbook-typst-doc
2 changes: 1 addition & 1 deletion example/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ src = "src"
title = "Typst Doc Example"

[output.html]
site-url = "/mdbook-typst-doc/"
# site-url = "/mdbook-typst-doc/"

# Include the recommeded .css file
additional-css = ["typst-doc.css"]
Expand Down
2 changes: 1 addition & 1 deletion example/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Summary

- [Main](./chapter_1.md)
- [Type Definition](./type_definition.md)
- [Type Definition](./type_definition.md)
7 changes: 2 additions & 5 deletions example/src/chapter_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ parbreak()
import "/data.typ": a
a
```

```typ
Expand All @@ -71,13 +69,12 @@ hello world
Hello this is a description {{#type int}}
</parameter-definition>

<parameter-definition default="(1,2,3)" name="stuff" types="array">
<parameter-definition name="not-default" types="array">
Some other information that is totally useful.
</parameter-definition>


<parameter-definition name="not-default" types="custom" default=""join me"">
<parameter-definition name="stuff" types="custom" default=""join me"">
Some other information that is totally useful.
</parameter-definition>


1 change: 1 addition & 0 deletions example/src/f/a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# next
8 changes: 6 additions & 2 deletions example/src/type_definition.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Custom {{#type custom}}
# {{#!type custom}}

Lorem ipsum...
This works without the link.

# {{#type custom}}

This is how it breaks if you don't turn off the link.
26 changes: 0 additions & 26 deletions example/themes/typst-doc/parameter.hbs

This file was deleted.

1 change: 0 additions & 1 deletion example/themes/typst-doc/type.hbs

This file was deleted.

2 changes: 1 addition & 1 deletion example/typst-doc.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/* Types */
.type > a {
text-decoration: none;
color: black;
color: black !important;
}

.type > a:hover {
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ int = {class = "num", link = "https://typst.app/docs/reference/foundations/int/"

Normally the preprocessor will panic if a type is used that is not in the config table. That is, unless the `default-type-class` key has been given, in which case an element with the given class and no link will be placed.

For some reason having a link in the default template breaks when used within a heading. You can place an exclamation mark `!` between the hash and the type (`{{#!type ...}}`) to not use a link. If you know/find a way to fix this without having to turn off the link please let me know :)

### Typst Code Blocks
Code blocks that have a language `typ` or `typc` can be processed. A `typ` block is in markup mode while a `typc` block is in code mode.

Expand Down Expand Up @@ -60,6 +62,7 @@ You can override the default look of the above features by providing handlebar t
- `link` The url to the type's definition
- `class` The css class to apply to the type
- `name` The name of the type.
- `use_link` A boolean that is false when `!` is added after the hash. It should not use the link if false.
- `code.hbs`: Template for a Typst code block with no options.
- `source` The highlighted code block.
- `render.hbs`: Template for a rendered Typst code block.
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct TypeConfig {
name: String,
link: String,
class: String,
pub use_link: bool,
}

impl TypeConfig {
Expand All @@ -41,6 +42,7 @@ impl TypeConfig {
.map(|v| value_to_string(v).unwrap())
.unwrap_or_default(),
class,
use_link: true
})
} else {
bail!("Malformed table for TypeConfig: {}", toml)
Expand Down Expand Up @@ -95,6 +97,7 @@ impl<'a> Config<'a> {
name: key.to_owned(),
link: String::new(),
class: class.clone(),
use_link: true
})
} else {
bail!("Unknown type {}", key)
Expand Down
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Preprocessor for TypstPreprocessor {
impl TypstPreprocessor {
pub fn new() -> Self {
TypstPreprocessor {
type_regex: Regex::new(r"(?m)\{\{#type (.*?)\}\}").unwrap(),
type_regex: Regex::new(r"(?m)\{\{#(!)?type (.*?)\}\}").unwrap(),
code_block_regex: Regex::new(r"(?msU)```(typ|typc)(?:,(render|example))?\r?\n(.*)```")
.unwrap(),
parameter_regex: Regex::new(r"(?msU)<parameter-definition(?:\s+default=\u{22}(?P<default>.*)\u{22}|\s+name=\u{22}(?P<name>.*)\u{22}|\s+types=\u{22}(?P<types>.*)\u{22})+\s*>(?P<description>.*)</parameter-definition>").unwrap()
Expand Down Expand Up @@ -114,10 +114,9 @@ impl TypstPreprocessor {
chapter.content = self
.type_regex
.replace_all(&chapter.content, |captures: &Captures| {
config
.handlebars
.render("type", &config.get_type(&captures[1]).unwrap())
.unwrap()
let mut data = config.get_type(&captures[2]).unwrap();
data.use_link = captures.get(1).is_none();
config.handlebars.render("type", &data).unwrap()
})
.to_string();

Expand Down
42 changes: 25 additions & 17 deletions src/themes/parameter.hbs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
<div>
<div class="parameter-details">
<div class="main-details">
<h4>{{name}}:</h4>
{{#each types}}
{{#unless @first}}
or
{{/unless}}
{{this}}
{{/each}}
</div>
{{#if default}}
<span class="parameter-default">Default: {{default}}</span>
{{/if}}
</div>

<p class="parameter-description">{{description}}</p>
<div class="parameter">
<div class="parameter parameter-details">
<div class="parameter main-details">
<h4>{{name}}:</h4>
{{#each types}}
{{#unless @first}}
or
{{/unless}}

{{this}}

{{/each}}
</div>
{{#if default}}

<span class="parameter parameter-default">Default:{{default}}</span>

{{/if}}
</div>

<div class="parameter-description">

{{description}}

</div>
</div>
2 changes: 1 addition & 1 deletion src/themes/type.hbs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a href='{{link}}' class='typst type type-{{class}}'>{{name}}</a>
<span class='typst type type-{{class}}'>{{#if use_link}}[{{name}}]({{link}}){{else}}{{name}}{{/if}}</span>

0 comments on commit 9037a33

Please sign in to comment.