Repository navigation
Closed
Labels
feature requestNew feature or requestgood contributionSuitable for contribution, but unlike "good first issue", it may need prior experiencepackagesAnything related to Typst package management.scriptingAbout Typst's coding capabilities
Description
Description
It would be highly convenient to allow full folders to exist as local packages:
#import "@local/my-templates/proposal/nsf.typ": *Currently, this results in the following error:
error: `templates/proposal/nsf` is not a valid package name
Use Case
I have one templates folder with several options:
templates
├───slides
| ├───dark.typ
| └───light.typ
└───proposal
├───nsf.typ
└───sbir.typ
Currently, the best workaround is to use a relative import:
#import "../../../templates/slides/dark.typ": *This is not ideal because (1) it is sensitive to the location of the main document and (2) requires typst to be run with --root to work.
I would love to symlink this folder to typst/packages/local and access it as a "package" to increase reusability. Even if I were to "publish" templates as a package to make this happen, it would still require "flattening" each template into something individually accessible since only one entrypoint can be specified:
#import "@local/templates:0.1.0": slides-dark
// Even so, this does not allow for star-imports of *only* the "slides/dark" template helpersIt seems like potential solutions are to:
- Allow multiple entrypoints
- Allow the entrypoint to be a keyword like "folder" or "none" or something similar that indicates there are subpackages
- Allow arbitrary folders to be placed in
localwithout complaining that they are nonconformant packages
Metadata
Metadata
Assignees
Labels
feature requestNew feature or requestgood contributionSuitable for contribution, but unlike "good first issue", it may need prior experiencepackagesAnything related to Typst package management.scriptingAbout Typst's coding capabilities
Activity
laurmaedje commentedon Sep 15, 2023
It was an intentional design decision that a package only exposes its definitions and not its directory tree. Otherwise, pretty much any change to a package would be potentially breaking. The current syntax isn't super convenient for nested packages, but you could have something like this:
We could think about a way to better support nested imports.
[-]Feature request: directory walking in packages[/-][+]Directly access package as directory[/+]ntjess commentedon Sep 15, 2023
That makes sense; I was thinking since packages versions must be pinned on import there's not a large worry of breaking changes. my background is Python, where by default all files and objects are reachable but authors use underscores in the name to denote internal utilities / unstable things. I suppose backwards compatibility will be more of a concern if #1632 is added
Alternatively, how do you feel about allowing
entrypointsin the toml to be an array? It might involve the following specification:#import package:0.1.0"is an error#import package:0.1.0/foo"specifies the "foo.typ" entrypoint (any other separator could do){default=""}indicates which entrypoint is used in the case of#import "package:0.1.0"{modules=[]}indicate candidates for array usageMore food for thought: Perhaps a
{datadir}/typst/path.conffile specifies additional directories that can be#imported.Of course these are mere suggestions; I don't know how much this increases complexity and you may have already eliminated these ideas in the past for reasons I didn't consider.
My main objective is to increase reusability of company-specific templates and utilities vs. a relative import. Your solution is an acceptable workaround in the meantime, though
laurmaedje commentedon Sep 15, 2023
What I like about the current setup is that the internal package organization is very flexible. Assuming we had better syntax to allow something like the following:
Would that satisfy your use case? Or do you still think direct file access has tangible benefits over this.
ntjess commentedon Sep 15, 2023
That works too! It has no verbosity overhead compared to raw file access 👍
(in contrast to the triple-import that is currently required)
laurmaedje commentedon Sep 15, 2023
Okay, I think we can make this a thing!
[-]Directly access package as directory[/-][+]Convenient syntax for nested import[/+]ntjess commentedon Sep 20, 2023
If I were to use
What would be the proper usage later?
In other words, is the variable bound to the last name in the chain or the fully qualified path? I have a preference toward the latter, since it matches what was specified by the user and they can simply combine it with an "as" import to get the other behavior:
This matches Python behavior FWIW.
laurmaedje commentedon Sep 20, 2023
I would say, it should definitely be the former, because if you want the latter you can just write
#import "@local/templates:0.1.0": slides.ntjess commentedon Sep 20, 2023
I am not a rust programmer, but through searching the repo I think this is where the change would occur? Is this something a novice can make reasonable progress on?
Thanks for the quick replies!
laurmaedje commentedon Sep 20, 2023
On the parser side, you've found the right place. You would also need adjustments in
ast.rsand maybekind.rson the syntax side. On the evaluation side, the relevant code is ineval/mod.rsin theapply_importsfunctions.I believe yes. You might hit some tricky spots, but if you've got a bit of time I think it should be doable. I'm also happy to help out if you hit any roadblocks or design questions (and the same holds true for the people in #contributors on Discord).
ramithuh commentedon Nov 16, 2023
Hi @ntjess! I noticed that you have a
nsf.typfile (which I hopefully assume is a proposal template for NSF)I came across a tweet [1] that was searching for such template 🥹
If it's a template that's sharable, perhaps it could be very helpful to the person searching for it on Twitter. Apologies if this comes off as prying (I tried searching "nsf" on discord and came across this issue)
[1] https://twitter.com/nomad421/status/1723912916833804351
ntjess commentedon Nov 17, 2023
@ramithuh I was inspired by your request to open source my existing NSF templates: https://github.com/ntjess/typst-nsf-templates.
I don't use twitter, but you are welcome to point them in that direction 🙂
PgBiel commentedon Apr 29, 2024
Will be working on this! (Commenting so I can be assigned 🙂)