A platform for community discussion. Free, open, simple.
FEATURE: add link rich editor extension (#31150)
Renato Atilio7210d29c1852
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.jsnew file mode 100644index 00000000000..ffc1523f535--- /dev/null+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js@@ -0,0 +1,195 @@+/** @type {RichEditorExtension} */+const extension = {+ markSpec: {+ link: {+ attrs: {+ href: {},+ title: { default: null },+ // same value from the markdown-it token+ // null for [link](...), "autolink" for <...>, and "linkify" for plain URLs+ markup: { default: null },+ attachment: { default: null },+ "data-orig-href": { default: null },+ },+ inclusive: false,+ parseDOM: [+ {+ tag: "a[href]",+ getAttrs(dom) {+ return {+ href: dom.getAttribute("href"),+ title: dom.getAttribute("title"),+ attachment: dom.classList.contains("attachment"),+ "data-orig-href": dom.getAttribute("data-orig-href"),+ };+ },+ },+ ],+ toDOM(node) {+ return [+ "a",+ {+ href: node.attrs.href,+ title: node.attrs.title,+ class: node.attrs.attachment ? "attachment" : undefined,+ "data-orig-href": node.attrs["data-orig-href"],+ },+ 0,+ ];+ },+ },+ },+ parse: {+ link: {+ mark: "link",+ getAttrs(tok, tokens, i) {+ const attachment = tokens[i + 1].content.endsWith("|attachment");+ if (attachment) {+ tokens[i + 1].content = tokens[i + 1].content.replace(+ /\|attachment$/,+ ""+ );+ }++ return {+ href: tok.attrGet("href"),+ title: tok.attrGet("title") || null,+ markup: tok.markup,+ attachment,+ "data-orig-href": tok.attrGet("data-orig-href"),+ };+ },+ },+ },+ serializeMark: {+ // override mark serializer to support "|attachment"+ link: {+ open(state, mark, parent, index) {+ state.linkMarkup =+ mark.attrs.markup ??+ (isPlainURL(mark, parent, index) ? "autolink" : null);++ if (state.linkMarkup === "autolink") {+ return "<";+ }++ if (state.linkMarkup === "linkify") {+ return "";+ }++ return "[";+ },+ close(state, mark) {+ let { linkMarkup } = state;+ state.linkMarkup = undefined;++ if (linkMarkup === "autolink") {+ return ">";+ }++ if (linkMarkup === "linkify") {+ return "";+ }++ const attachment = mark.attrs.attachment ? "|attachment" : "";+ const href =+ mark.attrs["data-orig-href"] ??+ mark.attrs.href.replace(/[()"]/g, "\\$&");+ const title = mark.attrs.title+ ? ` "${mark.attrs.title.replace(/"/g, '\\"')}"`+ : "";++ return `${attachment}](${href}${title})`;+ },+ mixable: true,+ },+ },+ inputRules: ({ schema, markInputRule }) =>+ markInputRule(+ /\[([^\]]+)]\(([^)\s]+)(?:\s+[“"']([^“"']+)[”"'])?\)$/,+ schema.marks.link,+ (match) => {+ return { href: match[2], title: match[3] };+ }+ ),+ plugins: ({ pmState: { Plugin }, pmModel: { Slice, Fragment }, utils }) =>+ new Plugin({+ props: {+ // Auto-linkify plain-text pasted URLs+ clipboardTextParser(text, $context, plain, view) {+ if (view.state.selection.empty || !utils.getLinkify().test(text)) {+ return;+ }++ const marks = $context.marks();+ const selectedText = view.state.doc.textBetween(+ view.state.selection.from,+ view.state.selection.to+ );+ const textNode = view.state.schema.text(selectedText, [+ ...marks,+ view.state.schema.marks.link.create({ href: text }),+ ]);+ return new Slice(Fragment.from(textNode), 0, 0);+ },++ // Auto-linkify rich content with a single text node that is a URL+ transformPasted(paste, view) {+ if (+ paste.content.childCount === 1 &&+ paste.content.firstChild.isText &&+ !paste.content.firstChild.marks.some(+ (mark) => mark.type.name === "link"+ )+ ) {+ const matches = utils+ .getLinkify()+ .match(paste.content.firstChild.text);+ const isFullMatch =+ matches &&+ matches.length === 1 &&+ matches[0].raw === paste.content.firstChild.text;++ if (!isFullMatch) {+ return paste;+ }++ const marks = view.state.selection.$head.marks();+ const originalText = view.state.doc.textBetween(+ view.state.selection.from,+ view.state.selection.to+ );++ const textNode = view.state.schema.text(originalText, [+ ...marks,+ view.state.schema.marks.link.create({+ href: paste.content.firstChild.text,+ }),+ ]);+ paste = new Slice(Fragment.from(textNode), 0, 0);+ }+ return paste;+ },+ },+ }),+};++function isPlainURL(link, parent, index) {+ if (link.attrs.title || !/^\w+:/.test(link.attrs.href)) {+ return false;+ }+ let content = parent.child(index);+ if (+ !content.isText ||+ content.text !== link.attrs.href ||+ content.marks[content.marks.length - 1] !== link+ ) {+ return false;+ }+ return (+ index === parent.childCount - 1 ||+ !link.isInSet(parent.child(index + 1).marks)+ );+}++export default extension;diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.jsindex 0c9cfcb3eaa..a46e86c8bb8 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js@@ -3,6 +3,7 @@ import emoji from "./emoji";import hashtag from "./hashtag";import heading from "./heading";import image from "./image";+import link from "./link";import mention from "./mention";import underline from "./underline";@@ -12,7 +13,15 @@ import underline from "./underline";** @type {RichEditorExtension[]}*/-const defaultExtensions = [emoji, image, heading, hashtag, mention, underline];+const defaultExtensions = [+ emoji,+ image,+ link,+ heading,+ hashtag,+ mention,+ underline,+];defaultExtensions.forEach(registerRichEditorExtension);diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/link-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/link-test.gjsnew file mode 100644index 00000000000..9ecda885104--- /dev/null+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/link-test.gjs@@ -0,0 +1,43 @@+import { module, test } from "qunit";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";++module(+ "Integration | Component | prosemirror-editor - link extension",+ function (hooks) {+ setupRenderingTest(hooks);++ Object.entries({+ "basic link": [+ "[Example](https://example.com)",+ '<p><a href="https://example.com">Example</a></p>',+ "[Example](https://example.com)",+ ],+ "link with title": [+ '[Example](https://example.com "Example Title")',+ '<p><a href="https://example.com" title="Example Title">Example</a></p>',+ '[Example](https://example.com "Example Title")',+ ],+ autolink: [+ "<https://example.com>",+ '<p><a href="https://example.com">https://example.com</a></p>',+ "<https://example.com>",+ ],+ "attachment link": [+ "[File|attachment](https://example.com/file.pdf)",+ '<p><a href="https://example.com/file.pdf" class="attachment">File</a></p>',+ "[File|attachment](https://example.com/file.pdf)",+ ],+ "attachment link with hash upload": [+ "[File|attachment](upload://some-hash)",+ '<p><a href="/404" class="attachment" data-orig-href="upload://some-hash">File</a></p>',+ "[File|attachment](upload://some-hash)",+ ],+ }).forEach(([name, [markdown, html, expectedMarkdown]]) => {+ test(name, async function (assert) {+ this.siteSettings.rich_editor = true;+ await testMarkdown(assert, markdown, html, expectedMarkdown);+ });+ });+ }+);