Clarify the path argument of the image element (#4892)

Co-authored-by: Laurenz <laurmaedje@gmail.com>
Co-authored-by: Martin Haug <mhaug@live.de>
This commit is contained in:
Dmytro Kravchenko-Loznia 2024-09-26 16:07:39 +02:00 committed by GitHub
parent d1379d8bd9
commit cf35c2f44c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 65 additions and 1 deletions

View File

@ -155,6 +155,8 @@ impl Plugin {
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to a WebAssembly file. /// Path to a WebAssembly file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
) -> SourceResult<Plugin> { ) -> SourceResult<Plugin> {
let Spanned { v: path, span } = path; let Spanned { v: path, span } = path;

View File

@ -22,6 +22,8 @@ pub fn cbor(
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to a CBOR file. /// Path to a CBOR file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: path, span } = path; let Spanned { v: path, span } = path;

View File

@ -29,6 +29,8 @@ pub fn csv(
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to a CSV file. /// Path to a CSV file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
/// The delimiter that separates columns in the CSV file. /// The delimiter that separates columns in the CSV file.
/// Must be a single ASCII character. /// Must be a single ASCII character.

View File

@ -54,6 +54,8 @@ pub fn json(
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to a JSON file. /// Path to a JSON file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: path, span } = path; let Spanned { v: path, span } = path;

View File

@ -27,6 +27,8 @@ pub fn read(
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to a file. /// Path to a file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
/// The encoding to read the file with. /// The encoding to read the file with.
/// ///

View File

@ -32,6 +32,8 @@ pub fn toml(
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to a TOML file. /// Path to a TOML file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: path, span } = path; let Spanned { v: path, span } = path;

View File

@ -61,6 +61,8 @@ pub fn xml(
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to an XML file. /// Path to an XML file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: path, span } = path; let Spanned { v: path, span } = path;

View File

@ -44,6 +44,8 @@ pub fn yaml(
/// The engine. /// The engine.
engine: &mut Engine, engine: &mut Engine,
/// Path to a YAML file. /// Path to a YAML file.
///
/// For more details, see the [Paths section]($syntax/#paths).
path: Spanned<EcoString>, path: Spanned<EcoString>,
) -> SourceResult<Value> { ) -> SourceResult<Value> {
let Spanned { v: path, span } = path; let Spanned { v: path, span } = path;

View File

@ -54,7 +54,9 @@ use crate::World;
/// [gh-svg]: https://github.com/typst/typst/issues?q=is%3Aopen+is%3Aissue+label%3Asvg /// [gh-svg]: https://github.com/typst/typst/issues?q=is%3Aopen+is%3Aissue+label%3Asvg
#[elem(scope, Show, LocalName, Figurable)] #[elem(scope, Show, LocalName, Figurable)]
pub struct ImageElem { pub struct ImageElem {
/// Path to an image file. /// Path to an image file
///
/// For more details, see the [Paths section]($syntax/#paths).
#[required] #[required]
#[parse( #[parse(
let Spanned { v: path, span } = let Spanned { v: path, span } =

View File

@ -167,3 +167,49 @@ sequence: `[\u{1f600}]`. The same kind of escape sequences also work in
I got an ice cream for I got an ice cream for
\$1.50! \u{1f600} \$1.50! \u{1f600}
``` ```
## Paths
Typst has various features that require a file path to reference external
resources such as images, Typst files, or data files. Paths are represented as
[strings]($str). There are two kinds of paths: Relative and absolute.
- A **relative path** searches from the location of the Typst file where the
feature is invoked. It is the default:
```typ
#image("images/logo.png")
```
- An **absolute path** searches from the _root_ of the project. It starts with a
leading `/`:
```typ
#image("/assets/logo.png")
```
### Project root
By default, the project root is the parent directory of the main Typst file.
For security reasons, you cannot read any files outside of the root directory.
If you want to set a specific folder as the root of your project, you can use
the CLI's `--root` flag. Make sure that the main file is contained in the
folder's subtree!
```bash
typst compile --root .. file.typ
```
In the web app, the project itself is the root directory. You can always read
all files within it, no matter which one is previewed (via the eye toggle next
to each Typst file in the file panel).
### Paths and packages
A package can only load files from its own directory. Within it, absolute paths
point to the package root, rather than the project root. For this reason, it
cannot directly load files from the project directory. If a package needs
resources from the project (such as a logo image), you must pass the already
loaded image, e.g. as a named parameter `{logo: image("mylogo.svg")}`. Note that
you can then still customize the image's appearance with a set rule within the
package.
In the future, paths might become a
[distinct type from strings](https://github.com/typst/typst/issues/971), so that
they can retain knowledge of where they were constructed. This way, resources
could be loaded from a different root.