link to user page on mention
This commit is contained in:
parent
3fa42caa4e
commit
d2562e5cff
|
@ -4,6 +4,7 @@ import gfm from 'remark-gfm'
|
||||||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
|
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
|
||||||
/* Use `…/dist/cjs/…` if you’re not in ESM! */
|
/* Use `…/dist/cjs/…` if you’re not in ESM! */
|
||||||
import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'
|
import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'
|
||||||
|
import mention from '../lib/remark-mention'
|
||||||
|
|
||||||
export default function Text ({ children }) {
|
export default function Text ({ children }) {
|
||||||
return (
|
return (
|
||||||
|
@ -35,7 +36,7 @@ export default function Text ({ children }) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
remarkPlugins={[gfm]}
|
remarkPlugins={[gfm, mention]}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</ReactMarkdown>
|
</ReactMarkdown>
|
||||||
|
|
|
@ -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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -20,10 +20,10 @@
|
||||||
"formik": "^2.2.6",
|
"formik": "^2.2.6",
|
||||||
"graphql": "^15.5.0",
|
"graphql": "^15.5.0",
|
||||||
"ln-service": "^51.7.0",
|
"ln-service": "^51.7.0",
|
||||||
|
"mdast-util-find-and-replace": "^1.1.1",
|
||||||
"next": "^10.2.3",
|
"next": "^10.2.3",
|
||||||
"next-auth": "^3.13.3",
|
"next-auth": "^3.13.3",
|
||||||
"next-seo": "^4.24.0",
|
"next-seo": "^4.24.0",
|
||||||
"node-webshot": "^1.0.4",
|
|
||||||
"pageres": "^6.2.3",
|
"pageres": "^6.2.3",
|
||||||
"prisma": "^2.25.0",
|
"prisma": "^2.25.0",
|
||||||
"qrcode.react": "^1.0.1",
|
"qrcode.react": "^1.0.1",
|
||||||
|
|
Loading…
Reference in New Issue