Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resource_link regex, make non-greedy #927

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was just to document the behavior #928 🤷

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