Skip to content

Commit

Permalink
Matching the metastring
Browse files Browse the repository at this point in the history
Which is not a URL as I wrongly
assumed in the previous release
  • Loading branch information
diemol committed May 25, 2023
1 parent d606164 commit 0536c1d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
28 changes: 13 additions & 15 deletions src/theme/ReferenceCodeBlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,22 @@ export function parseReference (ref: string): GitHubReference {
}
}

export function parseCustomization (ref: string): CustomizedLinkOptions {
export function parseCustomization (metastring: string | undefined): CustomizedLinkOptions {

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

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


const urlUseCustomStyling = url.searchParams.has('customStyling')
const urlNoteStyling = urlUseCustomStyling ? {} : noteStyle;
const customStylingMatch = metastring?.match(/customStyling/);
const refUseCustomStyling = customStylingMatch?.length === 1;
const refNoteStyling = customStylingMatch?.length === 1 ? {} : noteStyle;

return {
title: urlTitle,
linkText: urlLinkText,
noteStyling: urlNoteStyling,
useCustomStyling: urlUseCustomStyling
title: refTitle,
linkText: refLinkText,
noteStyling: refNoteStyling,
useCustomStyling: refUseCustomStyling
}
}

Expand Down Expand Up @@ -136,7 +134,7 @@ function ReferenceCode(props: ReferenceCodeBlockProps) {
fetchCode(codeSnippetDetails, fetchResultStateDispatcher)
}

const parsedCustomization = parseCustomization(props.children)
const parsedCustomization = parseCustomization(props.metastring)

const customProps = {
...props,
Expand Down
4 changes: 2 additions & 2 deletions src/theme/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export interface DispatchMessage {
}

export interface CustomizedLinkOptions {
title: string | null
linkText: string | null
title: string | undefined
linkText: string
noteStyling: React.CSSProperties
useCustomStyling: boolean
}
6 changes: 3 additions & 3 deletions tests/ReferenceCodeBlock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ test('should parse GitHub reference properly', () => {
})

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

Expand Down
12 changes: 6 additions & 6 deletions tests/__snapshots__/ReferenceCodeBlock.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Object {

exports[`should use custom reference link text 1`] = `
Object {
"linkText": "\\"Sample text\\"",
"linkText": "Sample text",
"noteStyling": Object {
"color": "#0E75DD",
"fontSize": ".9em",
Expand All @@ -60,14 +60,14 @@ Object {
"textAlign": "center",
"textDecoration": "underline",
},
"title": null,
"title": undefined,
"useCustomStyling": false,
}
`;

exports[`should use custom reference link text 2`] = `
Object {
"linkText": "\\"Sample text\\"",
"linkText": "Sample text",
"noteStyling": Object {
"color": "#0E75DD",
"fontSize": ".9em",
Expand All @@ -76,16 +76,16 @@ Object {
"textAlign": "center",
"textDecoration": "underline",
},
"title": "\\"Sample title\\"",
"title": "Sample title",
"useCustomStyling": false,
}
`;

exports[`should use custom reference link text 3`] = `
Object {
"linkText": "\\"Sample text\\"",
"linkText": "Sample text",
"noteStyling": Object {},
"title": "\\"Sample title\\"",
"title": "Sample title",
"useCustomStyling": true,
}
`;

0 comments on commit 0536c1d

Please sign in to comment.