Skip to content

Commit

Permalink
Merge pull request #6 from NearSocial/markdown-hashtags
Browse files Browse the repository at this point in the history
Add onHashtag to Markdown component
  • Loading branch information
Evgeny Kuzyakov authored Feb 27, 2023
2 parents b567464 + 146f28c commit 07182e2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.3.0

- Add support to hashtags for `Markdown` component. Expose `onHashtag` similar to `onMention`.

## 0.2.2

- Await for wallet selector before trying to build a commit message. This fixes an issue when the wallet selector is not ready before the commit message is initialized.
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "near-social-vm",
"version": "0.2.2",
"version": "0.3.0",
"description": "Near Social VM",
"main": "dist/index.js",
"files": [
Expand Down
15 changes: 10 additions & 5 deletions src/lib/components/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { tomorrow } from "react-syntax-highlighter/dist/esm/styles/prism";
import React from "react";
import mentions from "./remark/mentions";
import hashtags from "./remark/hashtags";

export const Markdown = (props) => {
const { onLinkClick, text, onMention, syntaxHighlighterProps, ...rest } = props;
const { onLinkClick, text, onMention, onHashtag, syntaxHighlighterProps, ...rest } = props;
return (
<ReactMarkdown
{...rest}
plugins={[gfm, mentions]}
plugins={[]}
rehypePlugins={[]}
remarkPlugins={[gfm, mentions, hashtags]}
children={text}
components={{
strong({ node, children, ...props }) {
if (onMention && node.properties?.accountId) {
return onMention(node.properties?.accountId);
} else if (onHashtag && node.properties?.hashtag) {
return onHashtag(node.properties?.hashtag);
}
return <strong {...props}>{children}</strong>;
},
Expand Down Expand Up @@ -55,8 +62,6 @@ export const Markdown = (props) => {
);
},
}}
>
{text}
</ReactMarkdown>
/>
);
};
33 changes: 33 additions & 0 deletions src/lib/components/remark/hashtags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { findAndReplace } from "mdast-util-find-and-replace";

const hashtagRegex =
/#(\w+)/gi;

export default function hashtags() {
function replace(value, hashtag, match) {
if (
/[\w`]/.test(match.input.charAt(match.index - 1)) ||
/[/\w`]/.test(match.input.charAt(match.index + value.length))
) {
return false;
}

let node = { type: "text", value };
node = {
type: "strong",
children: [node],
data: {
hProperties: { hashtag },
},
};

return node;
}

function transform(markdownAST) {
findAndReplace(markdownAST, hashtagRegex, replace);
return markdownAST;
}

return transform;
}

0 comments on commit 07182e2

Please sign in to comment.