Skip to content

Commit 0536c1d

Browse files
committed
Matching the metastring
Which is not a URL as I wrongly assumed in the previous release
1 parent d606164 commit 0536c1d

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

src/theme/ReferenceCodeBlock/index.tsx

+13-15
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,22 @@ export function parseReference (ref: string): GitHubReference {
5151
}
5252
}
5353

54-
export function parseCustomization (ref: string): CustomizedLinkOptions {
54+
export function parseCustomization (metastring: string | undefined): CustomizedLinkOptions {
5555

56-
const url = new URL(ref);
56+
const refTitle = metastring?.match(/title="(?<title>.*?)"/)?.groups?.title;
5757

58-
const urlTitle = url.searchParams.get('title');
59-
const urlLinkText = url.searchParams.has('referenceLinkText')
60-
? url.searchParams.get('referenceLinkText')
61-
: DEFAULT_LINK_TEXT;
58+
const refLinkMatch = metastring?.match(/referenceLinkText="(?<referenceLinkText>.*?)"/);
59+
const refLinkText = refLinkMatch?.groups?.referenceLinkText ?? DEFAULT_LINK_TEXT;
6260

63-
64-
const urlUseCustomStyling = url.searchParams.has('customStyling')
65-
const urlNoteStyling = urlUseCustomStyling ? {} : noteStyle;
66-
61+
const customStylingMatch = metastring?.match(/customStyling/);
62+
const refUseCustomStyling = customStylingMatch?.length === 1;
63+
const refNoteStyling = customStylingMatch?.length === 1 ? {} : noteStyle;
64+
6765
return {
68-
title: urlTitle,
69-
linkText: urlLinkText,
70-
noteStyling: urlNoteStyling,
71-
useCustomStyling: urlUseCustomStyling
66+
title: refTitle,
67+
linkText: refLinkText,
68+
noteStyling: refNoteStyling,
69+
useCustomStyling: refUseCustomStyling
7270
}
7371
}
7472

@@ -136,7 +134,7 @@ function ReferenceCode(props: ReferenceCodeBlockProps) {
136134
fetchCode(codeSnippetDetails, fetchResultStateDispatcher)
137135
}
138136

139-
const parsedCustomization = parseCustomization(props.children)
137+
const parsedCustomization = parseCustomization(props.metastring)
140138

141139
const customProps = {
142140
...props,

src/theme/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export interface DispatchMessage {
1919
}
2020

2121
export interface CustomizedLinkOptions {
22-
title: string | null
23-
linkText: string | null
22+
title: string | undefined
23+
linkText: string
2424
noteStyling: React.CSSProperties
2525
useCustomStyling: boolean
2626
}

tests/ReferenceCodeBlock.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ test('should parse GitHub reference properly', () => {
88
})
99

1010
test('should use custom reference link text', () => {
11-
expect(parseCustomization('https://github.com/saucelabs/docusaurus-theme-github-codeblock/blob/main/src/theme/ReferenceCodeBlock/index.tsx?referenceLinkText="Sample text"'))
11+
expect(parseCustomization('referenceLinkText="Sample text"'))
1212
.toMatchSnapshot()
13-
expect(parseCustomization('https://github.com/saucelabs/docusaurus-theme-github-codeblock/blob/main/src/theme/ReferenceCodeBlock/index.tsx?referenceLinkText="Sample text"&title="Sample title"'))
13+
expect(parseCustomization('referenceLinkText="Sample text" title="Sample title"'))
1414
.toMatchSnapshot()
15-
expect(parseCustomization('https://github.com/saucelabs/docusaurus-theme-github-codeblock/blob/main/src/theme/ReferenceCodeBlock/index.tsx?referenceLinkText="Sample text"&title="Sample title"&customStyling'))
15+
expect(parseCustomization('referenceLinkText="Sample text" title="Sample title" customStyling'))
1616
.toMatchSnapshot()
1717
})
1818

tests/__snapshots__/ReferenceCodeBlock.test.ts.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Object {
5151

5252
exports[`should use custom reference link text 1`] = `
5353
Object {
54-
"linkText": "\\"Sample text\\"",
54+
"linkText": "Sample text",
5555
"noteStyling": Object {
5656
"color": "#0E75DD",
5757
"fontSize": ".9em",
@@ -60,14 +60,14 @@ Object {
6060
"textAlign": "center",
6161
"textDecoration": "underline",
6262
},
63-
"title": null,
63+
"title": undefined,
6464
"useCustomStyling": false,
6565
}
6666
`;
6767

6868
exports[`should use custom reference link text 2`] = `
6969
Object {
70-
"linkText": "\\"Sample text\\"",
70+
"linkText": "Sample text",
7171
"noteStyling": Object {
7272
"color": "#0E75DD",
7373
"fontSize": ".9em",
@@ -76,16 +76,16 @@ Object {
7676
"textAlign": "center",
7777
"textDecoration": "underline",
7878
},
79-
"title": "\\"Sample title\\"",
79+
"title": "Sample title",
8080
"useCustomStyling": false,
8181
}
8282
`;
8383

8484
exports[`should use custom reference link text 3`] = `
8585
Object {
86-
"linkText": "\\"Sample text\\"",
86+
"linkText": "Sample text",
8787
"noteStyling": Object {},
88-
"title": "\\"Sample title\\"",
88+
"title": "Sample title",
8989
"useCustomStyling": true,
9090
}
9191
`;

0 commit comments

Comments
 (0)