Skip to content

Commit

Permalink
Merge pull request #927 from mitodl/regex-fix
Browse files Browse the repository at this point in the history
fix resource_link regex, make non-greedy
  • Loading branch information
ChristopherChudzicki authored Jan 24, 2022
2 parents b96926d + 81868bc commit f08f706
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
jest.mock("@ckeditor/ckeditor5-utils/src/version")

import ResourceLink from "@mitodl/ckeditor5-resource-link/src/link"
import Markdown from "./Markdown"
import Markdown, { MarkdownDataProcessor } from "./Markdown"
import { createTestEditor, markdownTest } from "./test_util"
import { turndownService } from "../turndown"

Expand Down Expand Up @@ -46,4 +46,23 @@ describe("ResourceLink plugin", () => {
'<p><a class="resource-link" data-uuid="asdfasdfasdfasdf">text here</a></p>'
)
})

it("should serialize multiple links to and from markdown", async () => {
const editor = await getEditor("")
markdownTest(
editor,
'dogs {{< resource_link uuid1 "woof" >}} cats {{< resource_link uuid2 "meow" >}}, cool',
'<p>dogs <a class="resource-link" data-uuid="uuid1">woof</a> cats <a class="resource-link" data-uuid="uuid2">meow</a>, cool</p>'
)
})

it("[BUG] does not behave well if link title ends in backslash", async () => {
const editor = await getEditor("")
const { md2html } = (editor.data
.processor as unknown) as MarkdownDataProcessor
expect(md2html('{{< resource_link uuid123 "bad \\" >}}')).toBe(
// This is wrong. Should not end in &lt;/a&gt;
'<p><a class="resource-link" data-uuid="uuid123">bad &lt;/a&gt;</p>'
)
})
})
12 changes: 10 additions & 2 deletions static/js/lib/ckeditor/plugins/ResourceLinkMarkdownSyntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import { editor } from "@ckeditor/ckeditor5-core"
import MarkdownSyntaxPlugin from "./MarkdownSyntaxPlugin"
import { TurndownRule } from "../../../types/ckeditor_markdown"

export const RESOURCE_LINK_SHORTCODE_REGEX = /{{< resource_link (\S+) "(.+)" >}}/g

import {
RESOURCE_LINK_CKEDITOR_CLASS,
RESOURCE_LINK
} from "@mitodl/ckeditor5-resource-link/src/constants"

/**
* (\S+) to match and capture the UUID
* "(.*?)" to match and capture the label text
*
* Limitations:
* - gets fooled by label texts that include literal `" >}}` values. For
* example, < resource_link uuid123 "silly " >}} link" >}}.
*/
export const RESOURCE_LINK_SHORTCODE_REGEX = /{{< resource_link (\S+) "(.*?)" >}}/g

/**
* Class for defining Markdown conversion rules for Resource links
*
Expand Down

0 comments on commit f08f706

Please sign in to comment.