From e3eff37acaa9eae6cb2458349169ece44c174f0c Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Sat, 22 May 2021 01:05:13 -0400 Subject: [PATCH] fix: Safari support for PrismJS env configuration Fixes a RegExp validation error caused when building and executing a RegExp with LookBehind syntax on Safari. This issue causes a white screen when running `npm start` on Safari browser. --- src/components/Markdown/env.ts | 54 ++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/components/Markdown/env.ts b/src/components/Markdown/env.ts index d769642..a5b5ad0 100644 --- a/src/components/Markdown/env.ts +++ b/src/components/Markdown/env.ts @@ -1,34 +1,36 @@ import Prism from "prismjs" -if (supportsLookBehind()) { - Prism.languages.env = { - property: { - pattern: /(.*.)(?==)/, - }, - string: { - pattern: /(?<==)(.*.)/g, - global: true, - }, - operator: { - pattern: /=/ - } - } -} else { - Prism.languages.env = { - property: { - pattern: /(.*.)(?==)/, - }, - operator: { - pattern: /=/ - } - } -} - +import type { Grammar } from "prismjs" function supportsLookBehind() { try { - return /(?<==)(.*)/.test("=hello") + const lookbehindRegexp = new RegExp("(?<==)(.*)"); + + return lookbehindRegexp.test("=hello"); } catch(e) { return false; } -} \ No newline at end of file +} + +function configurePrismJS() { + const config: Grammar = { + property: { + pattern: /(.*.)(?==)/, + }, + operator: { + pattern: /=/ + } + } + + if (supportsLookBehind()) { + /* eslint-disable-next-line id-blacklist */ + config.string = { + pattern: new RegExp("(?<==)(.*.)", "g"), + global: true, + } + } + + Prism.languages.env = config; +} + +configurePrismJS();