How to concatenate TypstStrings in Typstry.jl?

I’m trying to make a custom string macro that effectively adds some initial boilerplate to the start of a Typst string.

The effect would be

julia> T"important" == typst"""
#set `some pretty rule`
important
"""
true

I think the main problem is that I can’t seem “hijack” the @typst_str macro, meaning I cant send my string to the macro as a string, it always interperets it as a macro

macro T_str(s::String)
    s = "#set `some pretty rule` \n" * s
    @typst_str(s)
end
MethodError: no method matching var"@typst_str"(::LineNumberNode, ::Module, ::Expr)

My current workaround is to just copy the source code for the @typst_str macro and add a line at the top but that seems bad long term in the event of an update to Typstry.jl

1 Like

This should work,

macro T_str(s::String)
     :(@typst_str($("#set `some pretty rule` \n" * s)))
end

i.e., instead of calling the macro directly, generate code that passes it the updated string.

2 Likes

Alternatively, and required in a non-macro context, use TypstText (or some other type implementing show_typst) to print the value into the string.

julia> TypstString(TypstText("stuff"))
typst"stuff"
1 Like