Skip to content

Commit

Permalink
fix: Make mentionRegex stricter (#1)
Browse files Browse the repository at this point in the history
* use stricter regex

* remove leading whitespace from regex
  • Loading branch information
HoodieRocks authored Aug 11, 2023
1 parent 6088ae0 commit 3fbe703
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]


## What is this?

This package is a [unified][] ([remark][]) plugin to convert @ mentions to links: `@wooorm` -> `[**@wooorm**](https://github.com/wooorm)`.
This package is a [unified][unified-link] ([remark][remark-link]) plugin to convert @ mentions to links: `@wooorm` -> `[**@wooorm**](https://github.com/wooorm)`.

**unified** is a project that transforms content with abstract syntax trees
(ASTs).
Expand All @@ -19,7 +18,9 @@ This is a remark plugin that transforms mdast.
```sh
npm install remark-mentions
```

## Usage

```js
import {remark} from 'remark'
import remarkMentions from 'remark-mentions'
Expand All @@ -43,4 +44,8 @@ console.log(String(file))

[coverage-badge]: https://img.shields.io/codecov/c/github/finnrg/remark-mentions.svg

[coverage]: https://codecov.io/github/finnrg/remark-mentions
[coverage]: https://codecov.io/github/finnrg/remark-mentions

[unified-link]: https://github.com/unifiedjs/unified

[remark-link]: https://github.com/remarkjs/remark
32 changes: 25 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* @typedef {import('mdast').Root} Root
*
* @typedef {import('mdast').PhrasingContent} PhrasingContent
*
* @typedef {import('mdast-util-find-and-replace').ReplaceFunction} ReplaceFunction
*
Expand All @@ -10,9 +12,9 @@

import { findAndReplace } from "mdast-util-find-and-replace";

const userGroup = "[\\da-z][-\\da-z]{0,38}";
const userGroup = "[\\da-z][-\\da-z_]{0,38}";
const mentionRegex = new RegExp(
"@(" + userGroup + "(?:\\/" + userGroup + ")?)",
"(?:^|\\s)@(" + userGroup + ")",
"gi"
);

Expand All @@ -34,10 +36,26 @@ export default function remarkMentions(
* @param {string} username
*/
function replaceMention(value, username) {
return {
type: "link",
url: opts.usernameLink(username),
children: [{ type: "strong", children: [{ type: "text", value }] }],
};
/** @type {PhrasingContent[]} */
let whitespace = [];

// Separate leading white space
if (value.indexOf("@") > 0) {
whitespace.push({
type: "text",
value: value.substring(0, value.indexOf("@")),
});
}

return [
...whitespace,
{
type: "link",
url: opts.usernameLink(username),
children: [
{ type: "strong", children: [{ type: "text", value: value.trim() }] }, // Trim the username here
],
},
];
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { remark } from "remark";
import { VFile } from "vfile";

test("remark-mentions", (t) => {

t.equal(typeof remarkMentions, "function", "should be a function");

t.doesNotThrow(() => {
Expand All @@ -17,6 +18,8 @@ test("remark-mentions", (t) => {
t.equal(process("@test"), "[**@test**](/test)\n");

t.equal(process("This is @test"), "This is [**@test**](/test)\n");

t.equal(process("https://example.com/@test"), "https://example.com/@test\n");

t.equal(
process("@test", { usernameLink: (username) => `/Profile/${username}` }),
Expand Down

0 comments on commit 3fbe703

Please sign in to comment.