Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add a shared `LinkMatcher` type for the markdown linkifier (#44352)

Sérgio Saquetima10bb2c8fe54
diff --git a/frontend/discourse/app/lib/link-matcher.ts b/frontend/discourse/app/lib/link-matcher.ts
new file mode 100644
index 00000000000..05a0a2e6367
--- /dev/null
+++ b/frontend/discourse/app/lib/link-matcher.ts
@@ -0,0 +1,29 @@
+/** A link the markdown engine found in a piece of text. */
+export interface LinkMatch {
+ /** Where the link starts in the text. */
+ index: number;
+
+ /** Where the link ends in the text. */
+ lastIndex: number;
+
+ /** The link as written in the text. */
+ raw: string;
+
+ /** The normalized URL, with a protocol added when the text omitted one. */
+ url: string;
+}
+
+/**
+ * Finds links in plain text the way the markdown engine's linkifier does, so
+ * the composer detects the same links the cooked post will render.
+ */
+export interface LinkMatcher {
+ /** Whether the text contains a link. */
+ test(text: string): boolean;
+
+ /** Every link in the text, or `null` when there is none. */
+ match(text: string): LinkMatch[] | null;
+
+ /** The link at the very start of the text, or `null` when there is none. */
+ matchAtStart(text: string): LinkMatch | null;
+}
diff --git a/frontend/discourse/app/lib/text.js b/frontend/discourse/app/lib/text.js
index a80a068f26f..ce03eecbff4 100644
--- a/frontend/discourse/app/lib/text.js
+++ b/frontend/discourse/app/lib/text.js
@@ -35,6 +35,11 @@ export async function generateCookFunction(options) {
return await withEngine("generateCookFunction", options);
}
+/**
+ * Loads the markdown engine and returns its link matcher for `options`.
+ *
+ * @returns {Promise<import("discourse/lib/link-matcher").LinkMatcher>}
+ */
export async function generateLinkifyFunction(options) {
return await withEngine("generateLinkifyFunction", options);
}
diff --git a/frontend/discourse/app/lib/textarea-text-manipulation.ts b/frontend/discourse/app/lib/textarea-text-manipulation.ts
index db2bab24878..91f3f296412 100644
--- a/frontend/discourse/app/lib/textarea-text-manipulation.ts
+++ b/frontend/discourse/app/lib/textarea-text-manipulation.ts
@@ -22,6 +22,7 @@ import type {
import { bind } from "discourse/lib/decorators";
import { isTesting } from "discourse/lib/environment";
import escapeRegExp from "discourse/lib/escape-regexp";
+import type { LinkMatcher } from "discourse/lib/link-matcher";
import putCursorAtEnd from "discourse/lib/put-cursor-at-end";
import { generateLinkifyFunction } from "discourse/lib/text";
import { siteDir } from "discourse/lib/text-direction";
@@ -65,18 +66,6 @@ interface PlaceholderData {
processingPlaceholder?: string;
}
-interface LinkifyMatch {
- index: number;
- lastIndex: number;
- raw: string;
- url: string;
-}
-
-interface Linkify {
- test(text: string): boolean;
- match(text: string): LinkifyMatch[] | null;
-}
-
const INDENT_DIRECTION_LEFT = "left";
const INDENT_DIRECTION_RIGHT = "right";
@@ -127,7 +116,7 @@ export default class TextareaTextManipulation implements TextManipulation {
state = trackedObject<ToolbarState & Record<string, unknown>>({});
- _cachedLinkify?: Linkify;
+ _cachedLinkify?: LinkMatcher;
constructor(
owner: Owner,
diff --git a/frontend/discourse/app/static/markdown-it/index.js b/frontend/discourse/app/static/markdown-it/index.js
index 0c851c16fa5..5393c8132ce 100644
--- a/frontend/discourse/app/static/markdown-it/index.js
+++ b/frontend/discourse/app/static/markdown-it/index.js
@@ -23,6 +23,11 @@ export function generateCookFunction(options) {
return (text) => engine.cook(text);
}
+/**
+ * Builds the engine for `options` and returns its link matcher.
+ *
+ * @returns {import("discourse/lib/link-matcher").LinkMatcher}
+ */
export function generateLinkifyFunction(options) {
const engine = buildEngine(options);
return engine.linkify;
diff --git a/frontend/discourse/app/static/prosemirror/lib/markdown-it.js b/frontend/discourse/app/static/prosemirror/lib/markdown-it.js
index d6746ec0b96..970f69ec9a1 100644
--- a/frontend/discourse/app/static/prosemirror/lib/markdown-it.js
+++ b/frontend/discourse/app/static/prosemirror/lib/markdown-it.js
@@ -26,6 +26,7 @@ export function resetEngine() {
export const parse = (text) => getEngine().parse(text);
+/** @returns {import("discourse/lib/link-matcher").LinkMatcher} */
export const getLinkify = () => getEngine().linkify;
export const isWhiteSpace = (str, index) =>