Skip to content

Commit e12f557

Browse files
authored
Merge pull request #313 from andrewnicols/automaticTrackerLinks
[chore] Automatically convert tracker links
2 parents 8449ad2 + b96f5e4 commit e12f557

File tree

10 files changed

+994
-58
lines changed

10 files changed

+994
-58
lines changed

babel-test.config.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Moodle Pty Ltd.
3+
*
4+
* Moodle is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Moodle is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
module.exports = {
18+
presets: [
19+
require.resolve('@babel/preset-env'),
20+
],
21+
plugins: [
22+
['@babel/transform-runtime'],
23+
],
24+
};

babel.config.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* Copyright (c) Moodle Pty Ltd.
3+
*
4+
* Moodle is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Moodle is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
*/
117
module.exports = {
2-
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
18+
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
319
};

docs/apis/subsystems/form/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ The [disabledIf()](https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#
252252

253253
### hideIf()
254254

255+
import { Since } from '@site/src/components';
256+
255257
<Since version="3.4" />
256258

257259
The [hideif()](https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#hideIf) function can be used to conditionally _hide_ a group of elements, or and individual element depending on the state of other form elements.

general/app_releases/v3/v3.9.0.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ Learn more about our Pro and Premium subscriptions [here](https://moodle.com/app
8383
- [MOBILE-3407](https://tracker.moodle.org/browse/MOBILE-3407) - Drops aren't displayed in drag and drop into image questions in Moodle 3.6
8484
- [MOBILE-3420](https://tracker.moodle.org/browse/MOBILE-3420) - Blog images are not loaded properly
8585
- [MOBILE-3424](https://tracker.moodle.org/browse/MOBILE-3424) - Fix vulnerability in jQuery 1.9.1
86-
- [MOBILE-3438](https://tracker.moodle.org/browse/MOBILE-3438) - Update drag and drop code with changes on [MDL-58645](https://tracker.moodle.org/browse/MDL-58645]
86+
- [MOBILE-3438](https://tracker.moodle.org/browse/MOBILE-3438) - Update drag and drop code with changes on MDL-58645
8787
- [MOBILE-3443](https://tracker.moodle.org/browse/MOBILE-3443) - Big Modals on landscape like multichoice on database are not closable
8888
- [MOBILE-3448](https://tracker.moodle.org/browse/MOBILE-3448) - File name is not unique when uploading

general/releases/4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ moodleVersion: 4.0.0
88
description: The release notes for Moodle version 4.0.0.
99
---
1010

11+
import ReactPlayer from 'react-player/lazy';
1112
import { ReleaseNoteIntro } from '@site/src/components/ReleaseInformation';
1213

1314
<ReleaseNoteIntro releaseName={frontMatter.moodleVersion} />

jest.config.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) Moodle Pty Ltd.
3+
*
4+
* Moodle is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Moodle is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
module.exports = {
19+
transform: {
20+
'\\.[jt]sx?$': [
21+
'babel-jest', {
22+
configFile: './babel-test.config.js',
23+
},
24+
],
25+
},
26+
roots: [
27+
'<rootDir>/src',
28+
],
29+
};

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
},
6666
"devDependencies": {
6767
"@apidevtools/json-schema-ref-parser": "^9.0.9",
68+
"@babel/plugin-transform-runtime": "^7.18.9",
69+
"@babel/preset-env": "^7.18.9",
6870
"@docusaurus/module-type-aliases": "^2.0.0-beta.21",
6971
"@tsconfig/docusaurus": "^1.0.5",
7072
"@types/core-js": "^2.5.5",

src/remark/trackerLinks.js

+34-53
Original file line numberDiff line numberDiff line change
@@ -18,81 +18,63 @@
1818
/* eslint-disable-next-line import/no-extraneous-dependencies */
1919
const visit = require('unist-util-visit');
2020

21+
const projects = [
22+
'MDL',
23+
'MDLQA',
24+
'MDLSITE',
25+
'MOBILE',
26+
'CONTRIB',
27+
];
28+
29+
const expression = new RegExp(`(?<issueNumber>(${projects.join('|')})\\-\\d+)`, 'g');
30+
2131
/**
22-
* Update a text representation of a tracker issue into a link to that issue.
23-
*
24-
* These are in the format:
25-
*
26-
* {tracker MDL-12345}
32+
* Get the AST for a link pointing to the Moodle Tracker for the specified issue number.
2733
*
28-
* @param {Tree} node
29-
* @param {Number} index
30-
* @param {Tree} parent
34+
* @param {String} issueNumber
35+
* @returns {Tree}
3136
*/
32-
const updateTextLink = (node, index, parent) => {
33-
const { value } = node;
34-
const tokenStart = value.indexOf('{tracker ');
35-
if (tokenStart === -1) {
36-
return null;
37-
}
38-
39-
const linkStart = tokenStart + '{tracker '.length;
40-
const linkEnd = value.indexOf('}', linkStart);
41-
42-
const tokenEnd = linkEnd + 1;
43-
const issueNumber = value.substring(linkStart, linkEnd);
44-
45-
const newValue = value.substring(1, tokenStart) + issueNumber + value.substring(tokenEnd);
46-
console.warn(
47-
`The {tracker ${issueNumber}\` syntax has been deprecated.`
48-
+ `Please use a [${issueNumber}](https://tracker.moodle.org/browse/${issueNumber})`,
49-
);
50-
51-
parent.children.splice(index, 1, {
37+
const getLinkFromIssueNumber = (issueNumber) => ({
38+
type: 'link',
39+
url: `https://tracker.moodle.org/browse/${issueNumber}`,
40+
children: [{
5241
type: 'text',
53-
value: newValue,
54-
});
55-
56-
return null;
57-
};
42+
value: issueNumber,
43+
}],
44+
});
5845

5946
/**
60-
* Update an inline representation of a tracker issue into a link to that issue.
47+
* Update a text representation of a tracker issue into a link to that issue.
6148
*
6249
* These are in the format:
6350
*
64-
* {tracker}`MDL-12345`
51+
* [PROJECT]-12345
6552
*
6653
* @param {Tree} node
6754
* @param {Number} index
6855
* @param {Tree} parent
6956
*/
70-
const updateInlineLink = (node, index, parent) => {
71-
const tokenStart = node.value.indexOf('{tracker}');
72-
if (tokenStart === -1) {
73-
return null;
74-
}
57+
const updateTextLink = (node, index, parent) => {
58+
const { value } = node;
7559

76-
if (parent.children.length < index + 2) {
60+
if (parent.type === 'link') {
7761
return null;
7862
}
7963

80-
const followingNode = parent.children[index + 1];
81-
if (followingNode.type !== 'inlineCode') {
64+
const match = expression.exec(value);
65+
if (match === null) {
8266
return null;
8367
}
8468

85-
const issueNumber = followingNode.value;
69+
const tokenStart = match.index;
70+
const tokenEnd = match.index + match.groups.issueNumber.length;
8671

87-
const newValue = node.value.substring(0, tokenStart) + issueNumber;
88-
console.warn(
89-
`The {tracker}\`${issueNumber}\` syntax has been deprecated.`
90-
+ `Please use a [${newValue}](https://tracker.moodle.org/browse/${newValue})`,
91-
);
92-
93-
parent.children.splice(index, 2, {
72+
parent.children.splice(index, 1, {
73+
type: 'text',
74+
value: node.value.substring(0, tokenStart),
75+
}, getLinkFromIssueNumber(match.groups.issueNumber), {
9476
type: 'text',
95-
value: newValue,
77+
value: node.value.substring(tokenEnd),
9678
});
9779

9880
return null;
@@ -102,7 +84,6 @@ const plugin = () => {
10284
const transformer = async (ast) => {
10385
visit(ast, 'text', (node, index, parent) => {
10486
updateTextLink(node, index, parent);
105-
updateInlineLink(node, index, parent);
10687
});
10788
};
10889
return transformer;

src/remark/trackerLinks.test.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Copyright (c) Moodle Pty Ltd.
3+
*
4+
* Moodle is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Moodle is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
const module = require('./trackerLinks');
19+
20+
describe('trackerLinks', () => {
21+
it('must be a function', () => {
22+
expect(typeof module).toBe('function');
23+
});
24+
25+
it.concurrent.each([
26+
[
27+
'Simple link in text',
28+
{
29+
type: 'paragraph',
30+
children: [
31+
{
32+
type: 'text',
33+
value: 'See MDL-12345 for more information',
34+
},
35+
],
36+
},
37+
{
38+
type: 'paragraph',
39+
children: [
40+
{
41+
type: 'text',
42+
value: 'See ',
43+
},
44+
{
45+
type: 'link',
46+
url: 'https://tracker.moodle.org/browse/MDL-12345',
47+
children: [
48+
{
49+
type: 'text',
50+
value: 'MDL-12345',
51+
},
52+
],
53+
},
54+
{
55+
type: 'text',
56+
value: ' for more information',
57+
},
58+
],
59+
},
60+
],
61+
[
62+
'Does not update existing links',
63+
{
64+
type: 'paragraph',
65+
children: [
66+
{
67+
type: 'text',
68+
value: 'Do not modify ',
69+
},
70+
{
71+
type: 'link',
72+
url: 'https://example.com',
73+
children: [{
74+
type: 'text',
75+
value: 'MDL-12345',
76+
}],
77+
}, {
78+
type: 'text',
79+
value: 'that link',
80+
},
81+
],
82+
},
83+
{
84+
type: 'paragraph',
85+
children: [
86+
{
87+
type: 'text',
88+
value: 'Do not modify ',
89+
},
90+
{
91+
type: 'link',
92+
url: 'https://example.com',
93+
children: [{
94+
type: 'text',
95+
value: 'MDL-12345',
96+
}],
97+
}, {
98+
type: 'text',
99+
value: 'that link',
100+
},
101+
],
102+
},
103+
],
104+
])('updates tracker links for testcase "%s"', async (name, input, expected) => {
105+
const transformer = module();
106+
107+
const sampleTree = {
108+
type: 'root',
109+
children: [input],
110+
};
111+
112+
await transformer(sampleTree);
113+
expect(sampleTree.children[0]).toEqual(expected);
114+
});
115+
});

0 commit comments

Comments
 (0)