Skip to content

Commit cbc63c4

Browse files
committed
[repo] Convert MDL-\d+ to link
1 parent 6c34fb8 commit cbc63c4

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// For full details on all of these configuration settings:
19+
// https://github.com/DavidAnson/markdownlint-cli2#markdownlint-cli2cjs
20+
21+
const { getMigrationConfig } = require('../../markdownlint-cli2-config.cjs');
22+
const path = require('path');
23+
24+
module.exports = getMigrationConfig([
25+
path.join(__dirname, 'rule.js'),
26+
]);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
/* eslint-disable import/no-extraneous-dependencies */
19+
20+
const {
21+
addError,
22+
forEachLine,
23+
getLineMetadata,
24+
} = require('markdownlint-rule-helpers');
25+
26+
// Track these link types.
27+
const trackerIssueTypes = [
28+
'MDL-', // Moodle project.
29+
'MDLQA-', // QA Testing.
30+
'MDLSITE-', // Community sites.
31+
'MDLUX-', // UX issues for the Moodle project.
32+
'MOBILE-', // Mobile issues.
33+
'MUA-', // Moodle Users Association.
34+
'UX-', // UX issues.
35+
];
36+
37+
const issueTypes = trackerIssueTypes.join('|');
38+
const expression = new RegExp(`(?<issueNumber>(?:${issueTypes})\\d+)`, 'g');
39+
const linkSearch = /(?<markdownLink>\[[^\]]*\]\([^)]*\))/g;
40+
41+
const getAllLinkLocation = (line) => {
42+
const linkLocations = [];
43+
let searchResult = linkSearch.exec(line);
44+
if (!searchResult) {
45+
return linkLocations;
46+
}
47+
do {
48+
linkLocations.push({
49+
start: searchResult.index,
50+
end: searchResult.index + searchResult.groups.markdownLink.length,
51+
});
52+
searchResult = linkSearch.exec(line);
53+
} while (searchResult);
54+
55+
return linkLocations;
56+
};
57+
58+
const isTextInLink = (line, location) => {
59+
const linkLocations = getAllLinkLocation(line);
60+
return linkLocations.some(({ start, end }) => (location > start && location < end));
61+
};
62+
63+
module.exports = {
64+
names: ['convert-MDL-links'],
65+
description: 'Convert plain text matching the old tracker filter to Tracker links',
66+
tags: ['migration'],
67+
function: function lint(params, onError) {
68+
forEachLine(getLineMetadata(params), (line, lineIndex, inCode) => {
69+
if (inCode) {
70+
// Don't touch links in code blocks.
71+
return;
72+
}
73+
74+
const lineNumber = lineIndex + 1;
75+
76+
let matches = expression.exec(line);
77+
if (!matches) {
78+
return;
79+
}
80+
81+
do {
82+
const { issueNumber } = matches.groups;
83+
84+
if (isTextInLink(line, matches.index)) {
85+
// Don't touch issue numbers which are already in links.
86+
return;
87+
}
88+
89+
const fixInfo = {
90+
editColumn: matches.index + 1,
91+
deleteCount: issueNumber.length,
92+
insertText: `[${issueNumber}](https://tracker.moodle.org/browse/${issueNumber})`,
93+
};
94+
95+
addError(
96+
onError,
97+
lineNumber,
98+
issueNumber,
99+
fixInfo.insertText,
100+
[matches.index + 1, issueNumber.length],
101+
fixInfo,
102+
);
103+
104+
matches = expression.exec(line);
105+
} while (matches !== null);
106+
});
107+
},
108+
};

scripts/wikimedia-fetch.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18+
/* eslint-disable import/no-extraneous-dependencies */
1819
const yaml = require('js-yaml');
1920
const { exec } = require('child_process');
2021
const { program } = require('commander');
2122
const { writeFile } = require('fs/promises');
22-
23-
// eslint-disable-next-line import/no-extraneous-dependencies
2423
const inquirer = require('inquirer');
2524

2625
const {
@@ -166,6 +165,10 @@ program
166165
'04-externallinks',
167166
'05-wikilinks',
168167

168+
// Replace MDL-\d+ strings with links to the tracker.
169+
// This replaced the wikimedia filter to do the same.
170+
'06-trackerlinkfilter',
171+
169172
// Bold must be before italic
170173
'08-bold',
171174
'09-italic',

0 commit comments

Comments
 (0)