Skip to content

Commit 689ee11

Browse files
authored
feat: Add d.js docs link component (discordjs#396)
* refactor: Move branches code into a mixin * feat: Add DocsLink component * docs: Add contributing section explaining docs-link component
1 parent 54c7510 commit 689ee11

File tree

6 files changed

+124
-45
lines changed

6 files changed

+124
-45
lines changed

CONTRIBUTING.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ to fetch all messages in a channel
253253

254254
If you're on the `11.x` branch, you'd see "You can use `message.channel.fetchMessages()` to fetch all messages in a channel. Use the `inline` attribute to make content display inline with the content around it. Otherwise, it'll be displayed on its own line.
255255

256-
You can refer to the `guide/.vuepress/branches.js` file to see which values are valid to use for the `version` attribute.
256+
You can refer to the `guide/.vuepress/mixins/branches.js` file to see which values are valid to use for the `version` attribute.
257257

258258
#### Codeblocks and other markdown
259259

@@ -277,3 +277,31 @@ message.channel.messages.fetch();
277277

278278
</branch>
279279
```
280+
281+
### Discord.js documentation links
282+
283+
On pages where links to the discord.js documentation are used, you can use the `<docs-link>` component. Since the discord.js documentation is split into different categories and branches, the component allows you to supply the necessary info accordingly. The only required prop is `path`.
284+
285+
```md
286+
Main docs, branch version inherited from branch selector, `class/Client`:
287+
<docs-link path="class/Client">Link text</docs-link>
288+
<!-- Becomes: https://discord.js.org/#/docs/main/v11/class/Client -->
289+
290+
Main docs, stable branch (becomes "v12" due to the aliases set in `.vuepress/mixins/branches.js`), `class/Client`:
291+
<docs-link branch="stable" path="class/Client">Link text</docs-link>
292+
<!-- Becomes: https://discord.js.org/#/docs/main/v12/class/Client -->
293+
294+
Main docs, reply-prefix branch, `class/Client`:
295+
<docs-link section="main" branch="reply-prefix" path="class/Client">Link text</docs-link>
296+
<!-- Becomes: https://discord.js.org/#/docs/main/reply-prefix/class/Client -->
297+
298+
Commando docs, djs-v11 branch, `class/Command`:
299+
<docs-link section="commando" branch="djs-v11" path="class/Command">Link text</docs-link>
300+
<!-- Becomes: https://discord.js.org/#/docs/commando/djs-v11/class/Command -->
301+
302+
Collection docs, master branch (no `branch` prop set), `class/Collection?scrollTo=partition`:
303+
<docs-link section="collection" path="class/Collection?scrollTo=partition">Link text</docs-link>
304+
<!-- Becomes: https://discord.js.org/#/docs/collection/master/class/Collection?scrollTo=partition -->
305+
```
306+
307+
If the `section` prop is set to `main` (or omitted) and the `branch` prop is omitted, the `branch` prop will default to the version the user has set via the site's branch selector dropdown and update accordingly. If `section` is set to anything else and `branch` is omitted, the `branch` prop will default to `'master'`.

guide/.vuepress/branches.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

guide/.vuepress/components/Branch.vue

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77
<script>
88
import semver from 'semver';
9-
import branches from '../branches.js';
109
import eventBus from '../eventBus.js';
11-
12-
const [defaultBranch] = branches;
10+
import branches from '../mixins/branches.js';
1311
1412
export default {
1513
name: 'Branch',
14+
mixins: [branches],
1615
props: {
1716
version: {
1817
type: String,
@@ -23,27 +22,16 @@ export default {
2322
'default': false,
2423
},
2524
},
26-
data() {
27-
return {
28-
selectedBranch: defaultBranch.version,
29-
};
30-
},
3125
computed: {
3226
displayContent() {
3327
return semver.satisfies(semver.coerce(this.version), this.selectedBranch);
3428
},
3529
},
3630
mounted() {
37-
this.selectedBranch = localStorage.getItem('branch-version') || defaultBranch.version;
3831
eventBus.$on('branch-update', this.updateBranch);
3932
},
4033
destroyed() {
4134
eventBus.$off('branch-update', this.updateBranch);
4235
},
43-
methods: {
44-
updateBranch(branch) {
45-
this.selectedBranch = branch;
46-
},
47-
},
4836
};
4937
</script>

guide/.vuepress/components/BranchSelector.vue

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,27 @@
1010
</template>
1111

1212
<script>
13-
import branches from '../branches.js';
1413
import eventBus from '../eventBus.js';
15-
16-
const [defaultBranch] = branches;
14+
import branches from '../mixins/branches.js';
1715
1816
// `route.query` with `page.html?v=1#header` renders as `{ v: '1' }`
1917
// `route.query` with `page.html#header?v=1` renders as `{}`, and renders `route.hash` as `'#header?v=1'`
2018
// the former is plausible but the latter is more common
21-
function extractBranchVersion(query, hash) {
19+
function getVersionFromRoute({ query, hash }) {
2220
const versionRegex = /\?(?:v|version)=(.*)/;
23-
const getBranch = version => branches.find(branch => branch.aliases.includes(version) || branch.version === version);
24-
25-
if (query.v || query.version) return getBranch(query.v || query.version);
26-
if (hash.length && versionRegex.test(hash)) return getBranch(hash.match(versionRegex)[1]);
27-
return null;
21+
return query.v || query.version || (hash.length && versionRegex.test(hash) ? hash.match(versionRegex)[1] : null);
2822
}
2923
3024
export default {
3125
name: 'BranchSelector',
32-
data() {
33-
return {
34-
branches,
35-
selectedBranch: defaultBranch.version,
36-
};
37-
},
26+
mixins: [branches],
3827
mounted() {
39-
const branch = extractBranchVersion(this.$route.query, this.$route.hash);
28+
const branch = this.getBranch(getVersionFromRoute({ query: this.$route.query, hash: this.$route.hash }));
4029
4130
if (branch) {
4231
this.selectedBranch = branch.version;
4332
return this.updateBranch();
4433
}
45-
46-
this.selectedBranch = localStorage.getItem('branch-version') || defaultBranch.version;
4734
},
4835
methods: {
4936
updateBranch() {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<template>
2+
<a :href="link" target="_blank" rel="noopener noreferrer">
3+
<slot></slot> <outbound-link />
4+
</a>
5+
</template>
6+
7+
<script>
8+
import eventBus from '../eventBus.js';
9+
import branches from '../mixins/branches.js';
10+
11+
const baseURL = 'https://discord.js.org/#/docs';
12+
const docsSections = ['main', 'commando', 'collection', 'rpc'];
13+
14+
export default {
15+
name: 'DocsLink',
16+
mixins: [branches],
17+
props: {
18+
section: {
19+
type: String,
20+
'default': 'main',
21+
},
22+
branch: String,
23+
path: {
24+
type: String,
25+
required: true,
26+
},
27+
},
28+
computed: {
29+
link() {
30+
const guideSection = docsSections.find(section => section === this.section) || docsSections[0];
31+
32+
const branch = guideSection === 'main'
33+
? this.getBranch(this.branch)
34+
? this.formatBranch(this.getBranch(this.branch).version)
35+
: this.branch || this.formatBranch(this.selectedBranch)
36+
: this.branch || 'master';
37+
38+
return `${baseURL}/${guideSection}/${branch}/${this.path}`;
39+
},
40+
},
41+
mounted() {
42+
eventBus.$on('branch-update', this.updateBranch);
43+
},
44+
destroyed() {
45+
eventBus.$off('branch-update', this.updateBranch);
46+
},
47+
methods: {
48+
formatBranch(version) {
49+
return `v${version.slice(0, 2)}`;
50+
},
51+
},
52+
};
53+
</script>

guide/.vuepress/mixins/branches.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const branches = [
2+
{
3+
label: 'v12 (stable)',
4+
version: '12.x',
5+
aliases: ['12', 'stable'],
6+
},
7+
{
8+
label: 'v11',
9+
version: '11.x',
10+
aliases: ['11'],
11+
},
12+
];
13+
14+
const [defaultBranch] = branches;
15+
16+
export default {
17+
data() {
18+
return {
19+
branches,
20+
defaultBranch,
21+
selectedBranch: defaultBranch.version,
22+
};
23+
},
24+
mounted() {
25+
this.selectedBranch = localStorage.getItem('branch-version') || defaultBranch.version;
26+
},
27+
methods: {
28+
getBranch(version) {
29+
return this.branches.find(branch => branch.aliases.includes(version) || branch.version === version);
30+
},
31+
updateBranch(branch) {
32+
this.selectedBranch = branch;
33+
},
34+
},
35+
};

0 commit comments

Comments
 (0)