Skip to content
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

fix: allow ] in filename code block #2169

Merged
merged 12 commits into from
Jul 24, 2023
21 changes: 21 additions & 0 deletions playground/basic/components/content/ProseCode.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts" setup>
defineProps<{
code: string,
language?: string,
filename?: string,
highlights: number[],
meta?: string
}>()
</script>

<template>
<span v-if="filename"> {{ filename }} </span>
<slot />
</template>

<style>
pre code .line {
display: block;
min-height: 1rem;
}
</style>
2 changes: 1 addition & 1 deletion playground/basic/content/2.features/3.mdc-highlighter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: title
array:
- item
- itemKey: itemValue
object:
object:
name: object
version: 1
string: "string"
Expand Down
7 changes: 7 additions & 0 deletions playground/basic/content/2.features/8.custom-prose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```vue [[...page].vue]
<template>
<div>
<h1>Custom Prose</h1>
</div>
</template>
```
2 changes: 1 addition & 1 deletion src/runtime/markdown-parser/handler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function parseThematicBlock (lang: string) {

const language = lang.replace(/[{|[](.+)/, '').match(/^[^ \t]+(?=[ \t]|$)/)
const highlightTokens = lang.match(/{([^}]*)}/)
const filenameTokens = lang.match(/\[([^\]]*)\]/)
const filenameTokens = lang.match(/\[(.*)\]/) /** Allow ']' in filename. @see https://regex101.com/r/Hm0t5m/1 */
Copy link
Contributor

@nobkd nobkd Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we used [^\]] to allow using lists in the meta part.
Here's an example what would not work anymore if we match the longest string.

```language [filename]{hightlights} meta=[...]

It would match this content filename]{hightlights} meta=[... as the filename

Copy link
Contributor

@nobkd nobkd Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we could use something like \] in the markdown and prevent matching those somehow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh I understand. I do it too quickly!

Copy link
Contributor

@nobkd nobkd Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, that this might work: /\[((\\]|[^\]])*)\]/


This part (\\]|[^\]])* matches zero or one or a sequence of: \] or characters that are not ]

Only if our string has no more ] after a \] it terminates the match at a \].

So this works:

```ts [[...slug\].vue] meta=[]

=> [...slug\].vue as the first group match.
Then we only have to replace \] which in reality is \\] with ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but if there is no meta, this regex is not working.

https://regex101.com/r/bG0Net/1

EDIT: User must set a , I understand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const filenameTokens = lang.match(/\[(.*)\]/) /** Allow ']' in filename. @see https://regex101.com/r/Hm0t5m/1 */
const filenameTokens = lang.match(/\[((\\]|[^\]])*)\]/)

Copy link
Contributor

@nobkd nobkd Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And because of GitHub, I'm not allowed to suggest changes out of some range:

Change for L29:

-    filename: Array.isArray(filenameTokens) && filenameTokens[1] ? filenameTokens[1] : undefined,
+    filename: Array.isArray(filenameTokens) && filenameTokens[1] ? filenameTokens[1].replace(/\\]/g, ']') : undefined,

To remove the now redundant backslashes.

const meta = lang.replace(/^\w*\s*(\[[^\]]*\]|\{[^}]*\})?\s*(\[[^\]]*\]|\{[^}]*\})?\s*/, '')
Copy link
Contributor

@nobkd nobkd Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const meta = lang.replace(/^\w*\s*(\[[^\]]*\]|\{[^}]*\})?\s*(\[[^\]]*\]|\{[^}]*\})?\s*/, '')
const meta = lang.replace(/^\w*\s*(\[((\\]|[^\]])*)\]|\{[^}]*\})?\s*(\[((\\]|[^\]])*)\]|\{[^}]*\})?\s*/, '')

To replace the correct part, to have the meta left.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactor this to be more understandable and to reuse previous regex.


return {
Expand Down