From 0d83ecb06875089b8287302c5ed1ec1d58db7955 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Wed, 16 Nov 2022 13:52:39 +0800 Subject: [PATCH] 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. --- .../src/websites/github-handler.ts | 2 +- .../test/github-handler.test.ts | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 packages/content-handler/test/github-handler.test.ts diff --git a/packages/content-handler/src/websites/github-handler.ts b/packages/content-handler/src/websites/github-handler.ts index f086f72d3..7ac64c3fb 100644 --- a/packages/content-handler/src/websites/github-handler.ts +++ b/packages/content-handler/src/websites/github-handler.ts @@ -35,7 +35,7 @@ export class GitHubHandler extends ContentHandler { if (twitterTitle && twitterTitleContent) { twitterTitle.setAttribute( 'content', - twitterTitleContent.replace(/GitHub - .*\//, '') + twitterTitleContent.replace(/GitHub - (.*?)\//, '') ) } diff --git a/packages/content-handler/test/github-handler.test.ts b/packages/content-handler/test/github-handler.test.ts new file mode 100644 index 000000000..16cbb12b3 --- /dev/null +++ b/packages/content-handler/test/github-handler.test.ts @@ -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( + ` + + + + + +
this is the content of the article
+ + + ` + ) + 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`) + }) +})