Skip to content

Commit c542bde

Browse files
authored
Add improved docs
Closes GH-33. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
1 parent 05ed9ef commit c542bde

File tree

1 file changed

+194
-63
lines changed

1 file changed

+194
-63
lines changed

readme.md

Lines changed: 194 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,79 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**remark**][remark] plugin to automatically link references to commits, issues,
12-
pull-requests, and users, like in GitHub issues, PRs, and comments (see [Writing
13-
on GitHub][writing-on-github]).
14-
15-
## Note!
11+
[**remark**][remark] plugin to link references to commits, issues, and users,
12+
in the same way that GitHub does in comments, issues, PRs, and releases (see
13+
[Writing on GitHub][writing-on-github]).
1614

17-
This plugin is ready for the new parser in remark
18-
([`micromark`](https://github.com/micromark/micromark),
19-
see [`remarkjs/remark#536`](https://github.com/remarkjs/remark/pull/536)).
20-
Version 10 works with old (12) and new (13+) remark!
15+
## Contents
16+
17+
* [What is this?](#what-is-this)
18+
* [When should I use this?](#when-should-i-use-this)
19+
* [Install](#install)
20+
* [Use](#use)
21+
* [API](#api)
22+
* [`unified().use(remarkGithub[, options])`](#unifieduseremarkgithub-options)
23+
* [Examples](#examples)
24+
* [Example: `buildUrl`](#example-buildurl)
25+
* [Syntax](#syntax)
26+
* [Types](#types)
27+
* [Compatibility](#compatibility)
28+
* [Security](#security)
29+
* [Related](#related)
30+
* [Contribute](#contribute)
31+
* [License](#license)
32+
33+
## What is this?
34+
35+
This package is a [unified][] ([remark][]) plugin to link references to commits,
36+
issues, and users: `@wooorm` -> `[**@wooorm**](https://github.com/wooorm)`.
37+
38+
**unified** is a project that transforms content with abstract syntax trees
39+
(ASTs).
40+
**remark** adds support for markdown to unified.
41+
**mdast** is the markdown AST that remark uses.
42+
This is a remark plugin that transforms mdast.
43+
44+
## When should I use this?
45+
46+
This project is useful if you want to emulate how markdown would work in GitHub
47+
comments, issues, PRs, or releases, but it’s actually displayed somewhere else
48+
(on a website, or in other places on GitHub which don’t link references, such as
49+
markdown in a repo or Gist).
50+
This plugin does not support other platforms such as GitLab or Bitbucket and
51+
their custom features.
52+
53+
A different plugin, [`remark-gfm`][remark-gfm], adds support for GFM (GitHub
54+
Flavored Markdown).
55+
GFM is a set of extensions (autolink literals, footnotes, strikethrough, tables,
56+
and tasklists) to markdown that are supported everywhere on GitHub.
57+
58+
Another plugin, [`remark-breaks`][remark-breaks], turns soft line endings
59+
(enters) into hard breaks (`<br>`s).
2160

2261
## Install
2362

24-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
25-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
26-
27-
[npm][]:
63+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
64+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
2865

2966
```sh
3067
npm install remark-github
3168
```
3269

70+
In Deno with [Skypack][]:
71+
72+
```js
73+
import remarkGithub from 'https://cdn.skypack.dev/remark-github@11?dts'
74+
```
75+
76+
In browsers with [Skypack][]:
77+
78+
```html
79+
<script type="module">
80+
import remarkGithub from 'https://cdn.skypack.dev/remark-github@11?min'
81+
</script>
82+
```
83+
3384
## Use
3485

3586
Say we have the following file, `example.md`:
@@ -58,18 +109,21 @@ Some links:
58109
And our module, `example.js`, looks as follows:
59110

60111
```js
61-
import {readSync} from 'to-vfile'
112+
import {read} from 'to-vfile'
62113
import {remark} from 'remark'
114+
import remarkGfm from 'remark-gfm'
63115
import remarkGithub from 'remark-github'
64116

65-
const file = readSync('example.md')
117+
main()
118+
119+
async function main() {
120+
const file = await remark()
121+
.use(remarkGfm)
122+
.use(remarkGithub)
123+
.process(await read('example.md'))
66124

67-
remark()
68-
.use(remarkGithub)
69-
.process(file)
70-
.then((file) => {
71-
console.log(String(file))
72-
})
125+
console.log(String(file))
126+
}
73127
```
74128

75129
Now, running `node example` yields:
@@ -102,11 +156,93 @@ The default export is `remarkGithub`.
102156

103157
### `unified().use(remarkGithub[, options])`
104158

105-
Automatically link references to commits, issues, pull-requests, and users, like
106-
in GitHub issues, PRs, and comments (see
159+
Link references to users, commits, and issues, in the same way that GitHub does
160+
in comments, issues, PRs, and releases (see
107161
[Writing on GitHub][writing-on-github]).
108162

109-
###### Conversion
163+
##### `options`
164+
165+
Configuration (optional).
166+
167+
###### `options.repository`
168+
169+
Repository to link against (`string`, optional).
170+
Detected in Node.js from the `repository` field in `package.json` if not given.
171+
Should point to a GitHub repository, such as
172+
`'https://github.com/user/project.git'` or `'user/project'`.
173+
174+
###### `options.mentionStrong`
175+
176+
Wrap mentions in `strong` (`boolean`, default: `true`).
177+
This makes them render more like how GitHub styles them.
178+
But GitHub itself uses CSS instead of strong.
179+
180+
###### `options.buildUrl`
181+
182+
Change how (and whether) things are linked (`Function`, optional).
183+
This can be used to point links to GitHub Enterprise or other places.
184+
It’s called with the following parameters:
185+
186+
* `values` (`BuildUrlValues`)
187+
— info on the link to build
188+
* `defaultBuildUrl` (`(values: BuildUrlValues) => string`)
189+
— function that can be called to perform normal behavior
190+
191+
It should return the URL to use (`string`) or `false` to not create a link.
192+
193+
The following schemas are passed as `BuildUrlValues`:
194+
195+
* `{type: 'commit', user, project, hash}`
196+
* `{type: 'compare', user, project, base, compare}`
197+
* `{type: 'issue', user, project, no}`
198+
* `{type: 'mention', user}`
199+
200+
## Examples
201+
202+
## Example: `buildUrl`
203+
204+
A `buildUrl` can be passed to not link mentions.
205+
For example, by changing `example.js` from before like so:
206+
207+
```diff
208+
@@ -8,7 +8,11 @@ main()
209+
async function main() {
210+
const file = await remark()
211+
.use(remarkGfm)
212+
- .use(remarkGithub)
213+
+ .use(remarkGithub, {
214+
+ buildUrl(values, defaultBuildUrl) {
215+
+ return values.type === 'mention' ? false : defaultBuildUrl(values)
216+
+ }
217+
+ })
218+
.process(await read('example.md'))
219+
220+
console.log(String(file))
221+
```
222+
223+
To instead point mentions to a different place, change `example.js` like so:
224+
225+
```diff
226+
@@ -8,7 +8,13 @@ main()
227+
async function main() {
228+
const file = await remark()
229+
.use(remarkGfm)
230+
- .use(remarkGithub)
231+
+ .use(remarkGithub, {
232+
+ buildUrl(values, defaultBuildUrl) {
233+
+ return values.type === 'mention'
234+
+ ? `https://yourwebsite.com/${values.user}/`
235+
+ : defaultBuildUrl(values)
236+
+ }
237+
+ })
238+
.process(await read('example.md'))
239+
240+
console.log(String(file))
241+
```
242+
243+
## Syntax
244+
245+
The following references are supported:
110246

111247
* Commits:
112248
`1f2a4fb`[`1f2a4fb`][sha]
@@ -134,56 +270,41 @@ in GitHub issues, PRs, and comments (see
134270
* At-mentions:
135271
`@wooorm`[**@wooorm**][mention]
136272

137-
###### Repository
273+
Autolinks to these references are also transformed:
274+
`https://github.com/wooorm` -> `[**@wooorm**](https://github.com/wooorm)`
138275

139-
These links are generated relative to a project.
140-
In Node this is detected automatically by loading `package.json` and looking for
141-
a `repository` field.
142-
In the browser, or when overwriting this, you can pass a `repository` in
143-
`options`.
144-
The value of `repository` should be a URL to a GitHub repository, such as
145-
`'https://github.com/user/project.git'`, or only `'user/project'`.
276+
## Types
146277

147-
###### Mentions
278+
This package is fully typed with [TypeScript][].
279+
It exports an `Options` type, which specifies the interface of the accepted
280+
options.
281+
There are also `BuildUrl`, `BuildUrlValues`, `BuildUrlCommitValues`,
282+
`BuildUrlCompareValues`, `BuildUrlIssueValues`, `BuildUrlMentionValues`,
283+
and `DefaultBuildUrl` types exported.
148284

149-
By default, mentions are wrapped in `strong` nodes (that render to `<strong>` in
150-
HTML), to simulate the look of mentions on GitHub.
151-
However, this creates different HTML markup, as the GitHub site applies these
152-
styles using CSS.
153-
Pass `mentionStrong: false` to turn off this behavior.
285+
## Compatibility
154286

155-
##### Custom URLs
287+
Projects maintained by the unified collective are compatible with all maintained
288+
versions of Node.js.
289+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
290+
Our projects sometimes work with older versions, but this is not guaranteed.
156291

157-
By default we build URLs to public GitHub.
158-
You can overwrite them to point to GitHub Enterprise or other places by passing
159-
a `buildUrl`.
160-
That function is given an object with different values and the default
161-
`buildUrl`.
162-
If `buildUrl` returns `false`, the value is not linked.
163-
164-
```js
165-
remark()
166-
.use(remarkGithub, {
167-
// The fields in `values` depends on the kind reference:
168-
// {type: 'commit', user, project, hash}
169-
// {type: 'compare', user, project, base, compare}
170-
// {type: 'issue', user, project, no}
171-
// {type: 'mention', user}
172-
// You can return a URL, which will be used, or `false`, to not link.
173-
buildUrl(values, defaultBuildUrl) {
174-
return values.type === 'mention'
175-
? `https://yourwebsite.com/${values.user}/`
176-
: defaultBuildUrl(values)
177-
}
178-
})
179-
```
292+
This plugin works with `unified` version 6+ and `remark` version 7+.
180293

181294
## Security
182295

183296
Use of `remark-github` does not involve [**rehype**][rehype] ([**hast**][hast]).
184297
It does inject links based on user content, but those links only go to GitHub.
185298
There are no openings for [cross-site scripting (XSS)][xss] attacks.
186299

300+
## Related
301+
302+
* [`remark-gfm`][remark-gfm]
303+
— support GFM (autolink literals, footnotes, strikethrough, tables,
304+
tasklists)
305+
* [`remark-breaks`][remark-breaks]
306+
— support breaks without needing spaces or escapes (enters to `<br>`)
307+
187308
## Contribute
188309

189310
See [`contributing.md`][contributing] in [`remarkjs/.github`][health] for ways
@@ -228,6 +349,8 @@ abide by its terms.
228349

229350
[npm]: https://docs.npmjs.com/cli/install
230351

352+
[skypack]: https://www.skypack.dev
353+
231354
[health]: https://github.com/remarkjs/.github
232355

233356
[contributing]: https://github.com/remarkjs/.github/blob/HEAD/contributing.md
@@ -242,7 +365,9 @@ abide by its terms.
242365

243366
[remark]: https://github.com/remarkjs/remark
244367

245-
[writing-on-github]: https://help.github.com/articles/writing-on-github/#references
368+
[unified]: https://github.com/unifiedjs/unified
369+
370+
[writing-on-github]: https://docs.github.com/en/github/writing-on-github#references
246371

247372
[sha]: https://github.com/remarkjs/remark-github/commit/1f2a4fb8f88a0a98ea9d0c0522cd538a9898f921
248373

@@ -254,6 +379,12 @@ abide by its terms.
254379

255380
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
256381

382+
[typescript]: https://www.typescriptlang.org
383+
257384
[rehype]: https://github.com/rehypejs/rehype
258385

259386
[hast]: https://github.com/syntax-tree/hast
387+
388+
[remark-gfm]: https://github.com/remarkjs/remark-gfm
389+
390+
[remark-breaks]: https://github.com/remarkjs/remark-breaks

0 commit comments

Comments
 (0)