From 98fc55970a0ec3e3df7fa1ff67ef59b6ce9eae05 Mon Sep 17 00:00:00 2001 From: folland87 Date: Tue, 22 Oct 2024 14:44:50 +0200 Subject: [PATCH] fix(publication softwares): Escape regexp reserved characters --- .../publications/[id]/components/header/index.tsx | 4 ++-- .../[id]/components/softwares/index.tsx | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/client/src/pages/publications/[id]/components/header/index.tsx b/client/src/pages/publications/[id]/components/header/index.tsx index 357a6af8..1651bf7c 100644 --- a/client/src/pages/publications/[id]/components/header/index.tsx +++ b/client/src/pages/publications/[id]/components/header/index.tsx @@ -12,7 +12,7 @@ export default function PublicationsHeader({ data, authors, affiliations }) {
- {publicationTypeMapping[data.type]} + {publicationTypeMapping[data.type] && {publicationTypeMapping[data.type]}} {intl.formatMessage({ id: `publications.header.oa.${data.isOa ? "true" : "false"}` })} @@ -54,4 +54,4 @@ export default function PublicationsHeader({ data, authors, affiliations }) { )}
) -} \ No newline at end of file +} diff --git a/client/src/pages/publications/[id]/components/softwares/index.tsx b/client/src/pages/publications/[id]/components/softwares/index.tsx index c4cae6ed..f550df1d 100644 --- a/client/src/pages/publications/[id]/components/softwares/index.tsx +++ b/client/src/pages/publications/[id]/components/softwares/index.tsx @@ -22,9 +22,14 @@ export default function Softwares({ softwares }: { softwares: SoftwareMention[] const getSoftwareContexts = (softwareName: string, contexts?: string[]) => { if (!contexts.length) return null; const newContexts = contexts.map((context) => { - const regexp = new RegExp(softwareName, "ig") - const newContext = context.replace(regexp, `${softwareName}`); - return newContext; + const name = softwareName.replace(/[-[\]{}()*+?.,\\^$|#]/g, '\\$&') + try { + const regexp = new RegExp(name, "ig") + const newContext = context.replace(regexp, `${softwareName}`); + return newContext; + } catch (e) { + return context; + } }); const stringContexts = newContexts.reduce((acc, cur) => { if (acc.length === 0) return cur; @@ -84,4 +89,4 @@ export default function Softwares({ softwares }: { softwares: SoftwareMention[] ); -} \ No newline at end of file +}