fix(v2): fix code block title parsing, support multiple metastring attributes (#4541)

* feat: lazy match classic theme code block title

* feat: allow single quotes for classic theme code block title

* refactor(v2): extract parseCodeBlockTitle function

extract `parseCodeBlockTitle` function to add tests for title parsing

* test(v2): add tests for parseCodeBlockTitle

* move code block title parser to theme-common

Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
duanwilliam 2021-04-14 01:24:52 -07:00 committed by GitHub
parent 8119de105a
commit 1abadbaeca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 9 deletions

View file

@ -0,0 +1,54 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {parseCodeBlockTitle} from '../codeBlockUtils';
describe('parseCodeBlockTitle', () => {
test('should parse double quote delimited title', () => {
expect(parseCodeBlockTitle(`title="index.js"`)).toEqual(`index.js`);
});
test('should parse single quote delimited title', () => {
expect(parseCodeBlockTitle(`title='index.js'`)).toEqual(`index.js`);
});
test('should not parse mismatched quote delimiters', () => {
expect(parseCodeBlockTitle(`title="index.js'`)).toEqual(``);
});
test('should parse undefined metastring', () => {
expect(parseCodeBlockTitle(undefined)).toEqual(``);
});
test('should parse metastring with no title specified', () => {
expect(parseCodeBlockTitle(`{1,2-3}`)).toEqual(``);
});
test('should parse with multiple metadatas title first', () => {
expect(parseCodeBlockTitle(`title="index.js" label="JavaScript"`)).toEqual(
`index.js`,
);
});
test('should parse with multiple metadatas title last', () => {
expect(parseCodeBlockTitle(`label="JavaScript" title="index.js"`)).toEqual(
`index.js`,
);
});
test('should parse double quotes when delimited by single quotes', () => {
expect(parseCodeBlockTitle(`title='console.log("Hello, World!")'`)).toEqual(
`console.log("Hello, World!")`,
);
});
test('should parse single quotes when delimited by double quotes', () => {
expect(parseCodeBlockTitle(`title="console.log('Hello, World!')"`)).toEqual(
`console.log('Hello, World!')`,
);
});
});

View file

@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const codeBlockTitleRegex = /title=(["'])(.*?)\1/;
export function parseCodeBlockTitle(metastring?: string): string {
return metastring?.match(codeBlockTitleRegex)?.[2] ?? '';
}