Skip to content

Commit

Permalink
fix: handle code block meta string parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
haocheng6 committed Dec 28, 2023
1 parent 1b23507 commit f7f8141
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import parse from 'fenceparser';
const lineRangeRegex = /^L(\d+)(?:-L(\d+))?$/;

export function isCodeReference(meta: string): boolean {
// @ts-expect-error: the library definition is wrong.
return parse(meta).reference === true;
try {
// @ts-expect-error: the library definition is wrong.
return parse(meta).reference === true;
} catch {
return meta.endsWith(' reference');
}
}

export function parseLineRange(
Expand Down
3 changes: 2 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { getFixtureString } from './helpers.js';

describe('isCodeReference', () => {
it.each(['', 'random'])(
it.each(['', 'random', '[index.js]'])(
'returns false when the given meta string does not contain "reference"',
(input) => {
expect(isCodeReference(input)).toBe(false);
Expand All @@ -33,6 +33,7 @@ describe('isCodeReference', () => {
'reference',
'reference title="code title"',
'title="code title" reference',
'[index.js] reference',
])(
'returns true when the given meta string contains the "reference" property',
(input) => {
Expand Down

0 comments on commit f7f8141

Please sign in to comment.