Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: open links in new tab (#417)

Guilherme Oenning9f2807f5b606
diff --git a/public/components/common/MultiLineText.tsx b/public/components/common/MultiLineText.tsx
index 1124c9dce..94789f0b1 100644
--- a/public/components/common/MultiLineText.tsx
+++ b/public/components/common/MultiLineText.tsx
@@ -1,14 +1,5 @@
import * as React from "react";
-import * as md from "markdown-it";
-
-const full = md("commonmark", {
- html: false,
- breaks: true,
- linkify: true
-}).enable(["linkify", "strikethrough"]);
-const simple = md("commonmark", { html: false, breaks: true, linkify: true })
- .enable(["linkify", "strikethrough"])
- .disable(["heading", "image"]);
+import { markdown } from "@fider/services";
interface MultiLineText {
className?: string;
@@ -21,11 +12,8 @@ export const MultiLineText = (props: MultiLineText) => {
return <p />;
}
- const func = props.style === "full" ? full : simple;
+ const func = props.style === "full" ? markdown.full : markdown.simple;
return (
- <div
- className={`markdown-body ${props.className || ""}`}
- dangerouslySetInnerHTML={{ __html: func.render(props.text) }}
- />
+ <div className={`markdown-body ${props.className || ""}`} dangerouslySetInnerHTML={{ __html: func(props.text) }} />
);
};
diff --git a/public/services/index.ts b/public/services/index.ts
index 3cb84878f..448993da2 100644
--- a/public/services/index.ts
+++ b/public/services/index.ts
@@ -3,8 +3,9 @@ export * from "./cache";
export * from "./analytics";
export * from "./jwt";
export * from "./utils";
+import * as markdown from "./markdown";
import * as notify from "./notify";
import * as device from "./device";
import * as page from "./page";
import * as actions from "./actions";
-export { actions, page, device, notify };
+export { actions, page, device, notify, markdown };
diff --git a/public/services/markdown.spec.ts b/public/services/markdown.spec.ts
new file mode 100644
index 000000000..f91ffcc07
--- /dev/null
+++ b/public/services/markdown.spec.ts
@@ -0,0 +1,28 @@
+import * as markdown from "./markdown";
+
+const testCases = [
+ {
+ input: "Visit [GitHub](https://github.com) to learn more.",
+ expectedFull: '<p>Visit <a href="https://github.com" target="_blank">GitHub</a> to learn more.</p>',
+ expectedSimple: '<p>Visit <a href="https://github.com" target="_blank">GitHub</a> to learn more.</p>'
+ },
+ {
+ input: "My Picture ![](http://demo.dev.fider.io:3000/logo/100/28)",
+ expectedFull: '<p>My Picture <img src="http://demo.dev.fider.io:3000/logo/100/28" alt="" /></p>',
+ expectedSimple: '<p>My Picture !<a href="http://demo.dev.fider.io:3000/logo/100/28" target="_blank"></a></p>'
+ }
+];
+
+testCases.forEach(x => {
+ test(`Can parse markdown ${x.input} to ${x.expectedFull} (full mode)`, () => {
+ const result = markdown.full(x.input);
+ expect(result).toEqual(x.expectedFull);
+ });
+});
+
+testCases.forEach(x => {
+ test(`Can parse markdown ${x.input} to ${x.expectedSimple} (simple mode)`, () => {
+ const result = markdown.simple(x.input);
+ expect(result).toEqual(x.expectedSimple);
+ });
+});
diff --git a/public/services/markdown.ts b/public/services/markdown.ts
new file mode 100644
index 000000000..cf8d77873
--- /dev/null
+++ b/public/services/markdown.ts
@@ -0,0 +1,34 @@
+import * as md from "markdown-it";
+
+const fullMarkdown = md("commonmark", {
+ html: false,
+ breaks: true,
+ linkify: true
+}).enable(["linkify", "strikethrough"]);
+
+const simpleMarkdown = md("commonmark", { html: false, breaks: true, linkify: true })
+ .enable(["linkify", "strikethrough"])
+ .disable(["heading", "image"]);
+
+const linkOpen = (tokens: md.Token[], idx: number, options: any, env: any, self: md.Renderer) => {
+ const aIndex = tokens[idx].attrIndex("target");
+
+ if (aIndex < 0) {
+ tokens[idx].attrPush(["target", "_blank"]);
+ } else {
+ tokens[idx].attrs[aIndex][1] = "_blank";
+ }
+
+ return self.renderToken(tokens, idx, options);
+};
+
+fullMarkdown.renderer.rules.link_open = linkOpen;
+simpleMarkdown.renderer.rules.link_open = linkOpen;
+
+export const full = (input: string): string => {
+ return fullMarkdown.render(input).trim();
+};
+
+export const simple = (input: string): string => {
+ return simpleMarkdown.render(input).trim();
+};