-
-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
options.meta
does not return parsed metadata object in transformer
#792
Comments
You have to specify the parser with /**
* Custom meta string values
*/
const metaValues: MetaValue[] = [
{
name: 'title',
regex: /title="(?<value>[^"]*)"/,
},
{
name: 'custom',
regex: /custom="(?<value>[^"]+)"/,
},
];
const options = {
parseMetaString(meta) {
const map: Record<string, string> = {};
for (const value of metaValues) {
const result = value.regex.exec(meta);
if (result) {
map[value.name] = result[1];
}
}
return map;
},
}; |
Same for
The docs (linked in the original report) state that no additional work is required. |
I think it is more likely a docs problem, the linked section states: Transformers can also access markdown 'meta' strings It only tells you that transformers can access the string ( |
A minimal example to solve this problem (at least work as expected as this issue): // Tks for @olets code
function parseMetaString(str = '') {
return Object.fromEntries(
str.split(' ').reduce((acc: [string, string | true][], cur) => {
const matched = cur.match(/(.+)?=("(.+)"|'(.+)')$/)
if (matched === null) return acc
const key = matched[1]
const value = matched[3] || matched[4] || true
acc = [...acc, [key, value]]
return acc
}, [])
)
}
export const processMeta = (): ShikiTransformer => {
return {
name: 'shiki-transformer-process-meta',
preprocess() {
if (!this.options.meta) return
const rawMeta = this.options.meta?.__raw
if (!rawMeta) return
const meta = parseMetaString(rawMeta)
Object.assign(this.options.meta, meta)
}
}
}
// Then add transformer into your config, like:
const shikiConfig = {
// ...
transformers: [processMeta()]
} Works well on astro v4.x here. Reload configuration file is needed to sync any changes if functions are split and using by import. |
That parseMetaString is identical to what I authored in the stackblitz I shared (with the addition of annotating |
@olets I thought you're tagged as contributor, so code is shared for this repository (I hope I maybe can do the same). If you ensure to make own copyright of the code, I'll delete it soon. Very sorry. |
No need to delete it or stop using it, it isn't copyrighted and it isn't significantly different from what someone else could have come up with. I was just surprised to see something I wrote presented as if it was something someone else wrote. Glad it works for you. |
My aim is to provide a general enough solution to this problem that it can directly help others, just so. |
Validations
Describe the bug
I am trying to create a Shiki transformer that retrieves metadata like the filename from the
options.meta
object during the preprocessing step, but I'm encountering an issue.According to the Shiki documentation on transformers, the
options.meta
object should return a parsed key-value structure like:However, when I use
@shikijs/rehype
and attempt to accessoptions.meta
, I only receive the__raw
string without the parsed key-value pairs. For example:I expect
options.meta
to return:But instead, I get:
This forces me to manually parse the
__raw
string, which contradicts the documentation.Steps to Reproduce:
Use
@shikijs/rehype
with the following transformer:Process a code block with metadata (e.g.,
twoslash filename=app/routes/test.ts
).Expected Behavior:
options.meta
should return a parsed object with metadata fields likefilename
, along with the__raw
string.Actual Behavior:
Only the
__raw
string is returned, without parsed metadata.Environment:
@shikijs/rehype
and transformers (see below).Any insights on why the metadata isn't being parsed as expected would be greatly appreciated.
Reproduction
https://github.com/nikolailehbrink/shiki-rehype-bugs
Contributes
The text was updated successfully, but these errors were encountered: