Use greedy matching on the GitHub title regex

This fixes issues with titles that have slashes (/) in their title
as the regex match would look for the last possible matching
slash.
This commit is contained in:
Jackson Harper
2022-11-16 13:52:39 +08:00
parent 842c079756
commit 0d83ecb068
2 changed files with 33 additions and 1 deletions

View File

@ -35,7 +35,7 @@ export class GitHubHandler extends ContentHandler {
if (twitterTitle && twitterTitleContent) {
twitterTitle.setAttribute(
'content',
twitterTitleContent.replace(/GitHub - .*\//, '')
twitterTitleContent.replace(/GitHub - (.*?)\//, '')
)
}

View File

@ -0,0 +1,32 @@
import { GitHubHandler } from '../src/websites/github-handler'
import 'mocha'
import { expect } from 'chai'
import { parseHTML } from 'linkedom'
describe('preParse', () => {
it('should update the title on the page', async () => {
const dom = parseHTML(
`
<html>
<head>
<meta name="twitter:title"
content="GitHub - owner/repo: This is a title with a / char"
/>
</head>
<body>
<article>this is the content of the article</article>
</body>
</html>
`
)
const result = await new GitHubHandler().preParse(
'https://github.com/siyuan-note/siyuan',
dom.document
)
const title = result
.querySelector(`meta[name='twitter:title']`)
?.getAttribute('content')
expect(title).to.eq(`repo: This is a title with a / char`)
})
})