on master help sign in
Merge pull request #1474 from urbit/mp/external-links

Markdown: open external links in a new tab

commitea180668467f6878678fb68a49e8f9795d4310d1 authormatildepark <matilde.park@urbit.org> dateFri 6 May 2022, 14:39 (4 years ago) parent42f05984718e6c
2 files changed+41−8
components/Markdown.js+40−7 content/blog/layer-2-faq.md+1−1
components/Markdown.js+40−7
@@ -7,12 +7,12 @@ import normalize from "mdurl/encode";
77import merge from "deepmerge";
88import github from "hast-util-sanitize/lib/github";
99import remarkParse from "remark-parse";
10−import remarkRehype from "remark-rehype";
10+import remarkRehype, { defaultHandlers, all } from "remark-rehype";
1111import rehypeStringify from "rehype-stringify";
1212import rehypeRaw from "rehype-raw";
1313
1414// img is wrapped in figure so that images can be extra wide in the blog posts
15−function Img(h, node) {
15+function img(h, node) {
1616 var props = { src: normalize(node.url), alt: node.alt };
1717 if (node.title !== null && node.title !== undefined) {
1818 props.title = node.title;
@@ -31,10 +31,43 @@ function Img(h, node) {
3131 };
3232}
3333
34+function a(h, node) {
35+ const url = node.url;
36+
37+ if (
38+ url.includes("https://urbit.org") ||
39+ url.startsWith("/") ||
40+ url.startsWith("#")
41+ ) {
42+ return {
43+ type: "element",
44+ tagName: "a",
45+ properties: {
46+ href: url,
47+ },
48+ children: all(h, node),
49+ };
50+ } else {
51+ return {
52+ type: "element",
53+ tagName: "a",
54+ properties: {
55+ target: "_blank",
56+ rel: "noopener",
57+ href: url,
58+ },
59+ children: all(h, node),
60+ };
61+ }
62+}
63+
64+const handlers = defaultHandlers;
65+handlers.link = a;
66+handlers.image = img;
67+
3468const options = {
35− handlers: {
36− image: Img,
37− },
69+ allowDangerousHtml: true,
70+ handlers: handlers,
3871 sanitize: merge(github, {
3972 // remove user-content from github.json to remark-slug work as expected
4073 clobberPrefix: "",
@@ -45,14 +78,14 @@ const options = {
4578// Converts markdown strings into markdown HTML/React components
4679export default async function Markdown({ post }, disablePlugins) {
4780 const result = await remark()
48− .use(remarkParse, options)
81+ .use(remarkParse)
4982 .use(remarkprism, {
5083 plugins: !disablePlugins ? ["show-invisibles"] : [],
5184 })
5285 .use(gfm)
5386 .use(slug)
5487 .use(heading)
55− .use(remarkRehype, { allowDangerousHtml: true })
88+ .use(remarkRehype, options)
5689 .use(rehypeRaw)
5790 .use(rehypeStringify)
5891 .process(post.content);
content/blog/layer-2-faq.md+1−1
@@ -45,7 +45,7 @@ you **do not** need ETH to boot Urbit. That Urbit ID is now yours, forever.
4545Unsure whether you have been invited to claim a L2 or L1 planet? The quickest
4646way to identify a L2 planet is if you used the below flow to claim.
4747
48−![](/images/planet-l2-claim.gif)
48+<img src="/images/planet-l2-claim.gif" style="max-height: 400px; width: auto; margin: 0 auto;"/>
4949
5050For those with L1 planets, you can avoid future transaction fees by migrating
5151your L1 planet to L2. Read the migration instructions