Skip to content

Commit

Permalink
feat(typst): cross link support (#41)
Browse files Browse the repository at this point in the history
* feat: cross link support

* dev: encode label into url safe string

* docs: use cross links
  • Loading branch information
Myriad-Dreamin authored Dec 14, 2023
1 parent 1943685 commit c3efa80
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 18 deletions.
74 changes: 74 additions & 0 deletions contrib/typst/book/lib.typ
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,68 @@
)) <typst-book-build-meta>
]

#let link2page = state("typst-book-link2page", (:))

#let encode-url-component(s) = {
let prev = false
for (idx, c) in s.codepoints().enumerate() {
if c.starts-with(regex("[a-zA-Z]")) {
if prev {
prev = false
"-"
}
c
} else {
prev = true
if idx != 0 {
"-"
}
str(c.to-unicode())
}
}
}
#let cross-link-path-label(path) = {
assert(path.starts-with("/"), message: "absolute positioning required")
encode-url-component(path)
}

/// Cross link support
#let cross-link(path, reference: none, content) = {
let path-lbl = cross-link-path-label(path)
if reference != none {
assert(type(reference) == label, message: "invalid reference")
}

assert(content != none, message: "invalid label content")
locate(loc => {
let link-result = link2page.final(loc)
if path-lbl in link-result {
link((page: link-result.at(path-lbl), x: 0pt, y: 0pt), content)
return
}

if reference != none {
let result = query(reference, loc);
// whether it is internal link
if result.len() > 0 {
link(reference, content)
return
}
}
// assert(read(path) != none, message: "no such file")

link({
"cross-link://jump?path-label="
path-lbl
if reference != none {
"&label="
encode-url-component(str(reference))
}
}, content)
})
}

// Collect text content of element recursively into a single string
// https://discord.com/channels/1054443721975922748/1088371919725793360/1138586827708702810
#let plain-text(it) = {
Expand Down Expand Up @@ -317,6 +379,18 @@
}
}

show : it => {
let abs-link = cross-link-path-label("/" + link)
locate(loc => {
link2page.update(it => {
it.insert(abs-link, loc.page())
it
})
})

it
}

visit.at("chapter")(chapter-content)
}

Expand Down
1 change: 1 addition & 0 deletions contrib/typst/tidy-book/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// https://github.com/Jollywatt/arrow-diagrams
1 change: 1 addition & 0 deletions github-pages/docs/book.typ
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@
// re-export page template
#import "/contrib/typst/gh-pages.typ": project
#let book-page = project
#let cross-link = cross-link
6 changes: 3 additions & 3 deletions github-pages/docs/cli/init.typ
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#import "/github-pages/docs/book.typ": book-page
#import "/github-pages/docs/book.typ": book-page, cross-link

#show: book-page.with(title: "CLI Init Command")

= The init command

The `init` command will try to initialize your book to build your book successfully by default. It is hence including all of the #link("https://myriad-dreamin.github.io/typst-book/cli/build.html")[options] from `build` command.
The `init` command will try to initialize your book to build your book successfully by default. It is hence including all of the #cross-link("/cli/build.typ")[options] from `build` command.

For instance, Initialize a book to the directory `my-book`:

Expand All @@ -29,7 +29,7 @@ typst-book build my-book/ # memoryized dest-dir

== Things to note

The harder way, by creating the book without `init` command, your `book.typ` should at least provides a `book-meta`, as #link("https://myriad-dreamin.github.io/typst-book/guide/get-started.html")[Get Started] shown.
The harder way, by creating the book without `init` command, your `book.typ` should at least provides a `book-meta`, as #cross-link("/guide/get-started.typ")[Get Started] shown.

```typ
#import "@preview/book:0.2.2": *
Expand Down
16 changes: 8 additions & 8 deletions github-pages/docs/cli/main.typ
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#import "/github-pages/docs/book.typ": book-page
#import "/github-pages/docs/book.typ": book-page, cross-link

#show: book-page.with(title: "Command Line Tool")

= Command Line Tool

// todo: cross link
The `typst-book` command-line tool is used to create and build books.
After you have #link("https://myriad-dreamin.github.io/typst-book/guide/installation.html")[installed] `typst-book`, you can run the `typst-book help` command in your terminal to view the available commands.
After you have #cross-link("/guide/installation.typ")[installed] `typst-book`, you can run the `typst-book help` command in your terminal to view the available commands.

This following sections provide in-depth information on the different commands available.

// todo: cross link
- #link("https://myriad-dreamin.github.io/typst-book/cli/init.html")[`typst-book init <directory>`]Creates a new book with minimal boilerplate to start with.
- #link("https://myriad-dreamin.github.io/typst-book/cli/build.html")[`typst-book build`]Renders the book.
- #link("https://myriad-dreamin.github.io/typst-book/cli/serve.html")[`typst-book serve`]Runs a web server to view the book, and rebuilds on changes.
- #link("https://myriad-dreamin.github.io/typst-book/cli/clean.html")[`typst-book clean`]Deletes the rendered output.
- #link("https://myriad-dreamin.github.io/typst-book/cli/completions.html")[`typst-book completions`]Support for shell auto-completion.
- #cross-link("/cli/init.typ")[`typst-book init <directory>`]Creates a new book with minimal boilerplate to start with.
- #cross-link("/cli/build.typ")[`typst-book build`]Renders the book.
- #cross-link("/cli/serve.typ")[`typst-book serve`]Runs a web server to view the book, and rebuilds on changes.
- #cross-link("/cli/clean.typ")[`typst-book clean`]Deletes the rendered output.
- #cross-link("/cli/completions.typ")[`typst-book completions`]Support for shell auto-completion.

= Note about the missing `watch` command

We suggest you to use #link("https://github.com/Enter-tainer/typst-preview")[Typst Preview plugin] for preview feature. For more details, please see #link("https://myriad-dreamin.github.io/typst-book/guide/get-started.html")[Get Started] chapter.
We suggest you to use #link("https://github.com/Enter-tainer/typst-preview")[Typst Preview plugin] for preview feature. For more details, please see #cross-link("/guide/get-started.typ")[Get Started] chapter.
4 changes: 2 additions & 2 deletions github-pages/docs/cli/serve.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "/github-pages/docs/book.typ": book-page
#import "/github-pages/docs/book.typ": book-page, cross-link

#show: book-page.with(title: "CLI Serve Command")

Expand Down Expand Up @@ -30,7 +30,7 @@ typst-book serve path/to/book

== Build options

The `serve` command will build your book once before serving the content. It is hence including all of the #link("https://myriad-dreamin.github.io/typst-book/cli/build.html")[options] from `build` command.
The `serve` command will build your book once before serving the content. It is hence including all of the #cross-link("/cli/build.typ")[options] from `build` command.

== Server options

Expand Down
6 changes: 3 additions & 3 deletions github-pages/docs/format/book.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "/github-pages/docs/book.typ": book-page
#import "/github-pages/docs/book.typ": book-page, cross-link

#show: book-page.with(title: "book.typ")

Expand Down Expand Up @@ -41,7 +41,7 @@ In this example, you specify following fields for the book project:
- authors #type-hint("array<string>") (optional): Specify the author(s) of the book.
- summary #type-hint("content") (required): Summarize of the book.

See #link("https://myriad-dreamin.github.io/typst-book/format/book-meta.html")[Book Metadata] for more details.
See #cross-link("/format/book-meta.typ")[Book Metadata] for more details.

== build-meta

Expand All @@ -55,4 +55,4 @@ Specify build metadata of the book project. For example:

When you set `build-meta.dest-dir` to `../dist`, `typst-book` will output the generated content to `parent/to/book.typ/../../dist` or `parent/dist`.

See #link("https://myriad-dreamin.github.io/typst-book/format/build-meta.html")[Build Metadata] for more details.
See #cross-link("/format/build-meta.typ")[Build Metadata] for more details.
6 changes: 4 additions & 2 deletions github-pages/docs/format/theme.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "/github-pages/docs/book.typ": book-page
#import "/github-pages/docs/book.typ": book-page, cross-link, cross-link-path-label

#show: book-page.with(title: "Theme")

Expand All @@ -12,7 +12,9 @@ Currently we have no much design on theme's html part. But you can still configu

== Things to note

Your `book.typ` should at least provides a `book-meta`, as #link("https://myriad-dreamin.github.io/typst-book/guide/get-started.html")[Get Started] shown.
#let t = cross-link("/guide/get-started.typ")[Get Started]

Your `book.typ` should at least provides a `book-meta`, as #t shown.

```typ
#import "@preview/book:0.2.2": *
Expand Down

0 comments on commit c3efa80

Please sign in to comment.