From 028ab535df3e840bdf75c083ca7fbb275e0c61b3 Mon Sep 17 00:00:00 2001 From: Pedr Browne Date: Wed, 22 Apr 2020 17:41:06 +0100 Subject: [PATCH] fix: prevent escaping of footnotes and references (#3646) Prevent footnotes ([^1]:blah) and footnote references ([^1]) from being incorrectly escaped when switching between Markdown and Rich Text modes. Co-authored-by: Pedr --- .../__tests__/remarkEscapeMarkdownEntities.spec.js | 10 ++++++++-- .../src/serializers/remarkEscapeMarkdownEntities.js | 12 ++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/netlify-cms-widget-markdown/src/serializers/__tests__/remarkEscapeMarkdownEntities.spec.js b/packages/netlify-cms-widget-markdown/src/serializers/__tests__/remarkEscapeMarkdownEntities.spec.js index 86372ea4..a8336d14 100644 --- a/packages/netlify-cms-widget-markdown/src/serializers/__tests__/remarkEscapeMarkdownEntities.spec.js +++ b/packages/netlify-cms-widget-markdown/src/serializers/__tests__/remarkEscapeMarkdownEntities.spec.js @@ -73,7 +73,13 @@ describe('remarkEscapeMarkdownEntities', () => { expect(process('a b
*c*
d e')).toEqual('a b
*c*
d e'); }); - it('should not parse footnotes', () => { - expect(process('[^a]')).toEqual('\\[^a]'); + it('should not escape footnote references', () => { + expect(process('[^a]')).toEqual('[^a]'); + expect(process('[^1]')).toEqual('[^1]'); + }); + + it('should not escape footnotes', () => { + expect(process('[^a]:')).toEqual('[^a]:'); + expect(process('[^1]:')).toEqual('[^1]:'); }); }); diff --git a/packages/netlify-cms-widget-markdown/src/serializers/remarkEscapeMarkdownEntities.js b/packages/netlify-cms-widget-markdown/src/serializers/remarkEscapeMarkdownEntities.js index a9e0609b..8c696a48 100644 --- a/packages/netlify-cms-widget-markdown/src/serializers/remarkEscapeMarkdownEntities.js +++ b/packages/netlify-cms-widget-markdown/src/serializers/remarkEscapeMarkdownEntities.js @@ -124,14 +124,14 @@ const escapePatterns = [ /(`+)[^`]*(\1)/g, /** - * Links, Images, References, and Footnotes + * Links and Images * - * Match strings surrounded by brackets. This could be improved to - * specifically match only the exact syntax of each covered entity, but - * doing so through current approach would incur a considerable performance - * penalty. + * Match strings surrounded by square brackets, except when the opening + * bracket is followed by a caret. This could be improved to specifically + * match only the exact syntax of each covered entity, but doing so through + * current approach would incur a considerable performance penalty. */ - /(\[)[^\]]*]/g, + /(\[(?!\^)+)[^\]]*]/g, ]; /**