Skip to content

Commit

Permalink
markdown-plain-urls
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Nov 7, 2023
1 parent 3e3744f commit 094227b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"lodash": "^4.17.20",
"mathjax-full": "^3.1.0",
"md5": "^2.3.0",
"mdast-util-find-and-replace": "1.1.1",
"mdast-util-from-markdown": "^0.8.0",
"minimist": "^1.2.6",
"mobx": "^5.15.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TextWrap } from "../TextWrap/TextWrap.js"
import fromMarkdown from "mdast-util-from-markdown"
import type { Root, Content } from "mdast"
import { match } from "ts-pattern"
import { urlRegex } from "../markdown/remarkPlainLinks.js"

const SUPERSCRIPT_NUMERALS = {
"0": "\u2070",
Expand Down Expand Up @@ -888,12 +889,13 @@ function convertMarkdownNodeToIRTokens(
(item) => {
const splitted = item.value.split(/\s+/)
const tokens = splitted.flatMap((text, i) => {
const textNode = new IRText(text, fontParams)
const node = text.match(urlRegex)
? new IRLink(text, [textNode], fontParams)
: textNode
if (i < splitted.length - 1) {
return [
new IRText(text, fontParams),
new IRWhitespace(fontParams),
]
} else return [new IRText(text, fontParams)]
return [node, new IRWhitespace(fontParams)]
} else return [node]
})
return tokens
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react"
import { computed } from "mobx"
import { Remark } from "react-remark"
import { remarkPlainLinks } from "./markdown/remarkPlainLinks.js"

type SimpleMarkdownTextProps = {
text: string
Expand All @@ -12,6 +13,6 @@ export class SimpleMarkdownText extends React.Component<SimpleMarkdownTextProps>
}

render(): JSX.Element | null {
return <Remark>{this.text}</Remark>
return <Remark remarkPlugins={[remarkPlainLinks]}>{this.text}</Remark>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "mdast-util-find-and-replace"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import findAndReplace from "mdast-util-find-and-replace"

export const urlRegex = /https?:\/\/([\w-]+\.)+[\w-]+(\/[\w\- .\+/?:%&=~#]*)?/

export function remarkPlainLinks() {
const turnIntoLink = (value: any, _match: string) => {
return [
{
type: "link",
url: value,
children: [
{
type: "text",
value: value,
},
],
},
]
}
return (tree: any) => {
findAndReplace(tree, [[urlRegex, turnIntoLink]])
}
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11255,6 +11255,7 @@ __metadata:
lodash: "npm:^4.17.20"
mathjax-full: "npm:^3.1.0"
md5: "npm:^2.3.0"
mdast-util-find-and-replace: "npm:1.1.1"
mdast-util-from-markdown: "npm:^0.8.0"
minimist: "npm:^1.2.6"
mobx: "npm:^5.15.7"
Expand Down Expand Up @@ -14234,6 +14235,17 @@ __metadata:
languageName: node
linkType: hard

"mdast-util-find-and-replace@npm:1.1.1":
version: 1.1.1
resolution: "mdast-util-find-and-replace@npm:1.1.1"
dependencies:
escape-string-regexp: "npm:^4.0.0"
unist-util-is: "npm:^4.0.0"
unist-util-visit-parents: "npm:^3.0.0"
checksum: e4c9e50d9bce5ae4c728a925bd60080b94d16aaa312c27e2b70b16ddc29a5d0a0844d6e18efaef08aeb22c68303ec528f20183d1b0420504a0c2c1710cebd76f
languageName: node
linkType: hard

"mdast-util-from-markdown@npm:^0.8.0":
version: 0.8.5
resolution: "mdast-util-from-markdown@npm:0.8.5"
Expand Down

0 comments on commit 094227b

Please sign in to comment.