link to user page on mention

This commit is contained in:
keyan 2021-07-15 14:56:23 -05:00
parent 3fa42caa4e
commit d2562e5cff
4 changed files with 74 additions and 1064 deletions

View File

@ -4,6 +4,7 @@ import gfm from 'remark-gfm'
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
/* Use `…/dist/cjs/…` if youre not in ESM! */
import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'
import mention from '../lib/remark-mention'
export default function Text ({ children }) {
return (
@ -35,7 +36,7 @@ export default function Text ({ children }) {
)
}
}}
remarkPlugins={[gfm]}
remarkPlugins={[gfm, mention]}
>
{children}
</ReactMarkdown>

42
lib/remark-mention.js Normal file
View File

@ -0,0 +1,42 @@
import findAndReplace from 'mdast-util-find-and-replace'
// Username may only contain alphanumeric characters or single hyphens, and
// cannot begin or end with a hyphen*.
//
// \* That is: until <https://github.com/remarkjs/remark-github/issues/13>.
const userGroup = '[\\w_]+'
const mentionRegex = new RegExp(
'@(' + userGroup + '(?:\\/' + userGroup + ')?)',
'gi'
)
export default function mention (options) {
return function transformer (tree) {
findAndReplace(
tree,
[
[mentionRegex, replaceMention]
],
{ ignore: ['link', 'linkReference'] }
)
}
function replaceMention (value, username, match) {
if (
/[\w`]/.test(match.input.charAt(match.index - 1)) ||
/[/\w`]/.test(match.input.charAt(match.index + value.length))
) {
return false
}
const node = { type: 'text', value: value }
return {
type: 'link',
title: null,
url: '/' + username,
children: [node]
}
}
}

1091
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -20,10 +20,10 @@
"formik": "^2.2.6",
"graphql": "^15.5.0",
"ln-service": "^51.7.0",
"mdast-util-find-and-replace": "^1.1.1",
"next": "^10.2.3",
"next-auth": "^3.13.3",
"next-seo": "^4.24.0",
"node-webshot": "^1.0.4",
"pageres": "^6.2.3",
"prisma": "^2.25.0",
"qrcode.react": "^1.0.1",