vuepress / core

Vue-Powered Static Site Generator
https://vuepress.vuejs.org
MIT License
2.17k stars 922 forks source link

fix(markdown): use non-greedy matching when parsing attributes #1469

Closed pengzhanbo closed 5 months ago

pengzhanbo commented 5 months ago

Before submitting the PR, please make sure you do the following

What is the purpose of this pull request?

Description

When parsing code attrs, due to the greedy matching adopted in the current regular expression, the result of matching will be matched to the content that does not belong to it when there are multiple attrs.

```ts title="config/index.ts" foo="foo"
// more

```ts
const title = resolveAttr(info, 'title')
console.log(title)  // output -> config/index.ts" foo="foo

Modify the regular expression:

- const pattern = `\\b${attr}\\s*=\\s*(?<quote>['"])(?<content>.+)\\k<quote>(\\s|$)`
+ const pattern = `\\b${attr}\\s*=\\s*(?<quote>['"])(?<content>.+?)\\k<quote>(\\s|$)`

Result:

const title = resolveAttr(info, 'title')
console.log(title)  // output -> config/index.ts

close #1277 #1456

Screenshots

Before

After

Mister-Hope commented 5 months ago

try to fix the ci?

coveralls commented 5 months ago

Pull Request Test Coverage Report for Build 7637598102


Totals Coverage Status
Change from base Build 7636570246: 0.0%
Covered Lines: 676
Relevant Lines: 1700

💛 - Coveralls
meteorlxy commented 5 months ago

try to fix the ci?

@Mister-Hope I have no idea why the e2e workflow would randomly time out. Increasing the timeout config does not help. It should somehow get stuck 🤔

meteorlxy commented 5 months ago

@pengzhanbo Thanks for your contributions! ❤️