Skip to content

Commit

Permalink
[repo] Add markdownlint rule to identify obsolete pages
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed May 12, 2022
1 parent ce5312e commit e8b5a32
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
69 changes: 69 additions & 0 deletions .markdownlint/find-obsolete-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (c) Moodle Pty Ltd.
*
* Moodle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Moodle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
*/

/* eslint-disable import/no-extraneous-dependencies */

const {
addError,
forEachLine,
getLineMetadata,
} = require('markdownlint-rule-helpers');

const { isObsolete } = require('../migratedPages');

// eslint-disable-next-line max-len
const linkFinder = /(?<description>\[.*\](?=(?<link>\(https:\/\/docs.moodle.org\/dev\/(?<target>[^)]*)\)|\[[^\]]*\])))/g;

module.exports = {
names: ['MDLDOC004', 'find-obsolete-links'],
description: 'Identify obsolete links',
tags: ['migration'],
function: function MDLDOC004(params, onError) {
forEachLine(getLineMetadata(params), (line, lineIndex, inCode) => {
if (inCode) {
// Do not make changes to code stanzas.
return;
}

const lineNumber = lineIndex + 1;
let matches = linkFinder.exec(line);
if (!matches) {
return;
}
do {
const { target } = matches.groups;
if (!target) {
break;
}
const [targetPage] = target.split('#');
const context = `${matches.groups.description}${matches.groups.link}`;
if (isObsolete(targetPage)) {
addError(
onError,
lineNumber,
target,
context,
[matches.index + 1, context.length],
null,
);
}

matches = linkFinder.exec(line);
} while (matches !== null);
});
},
};
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"markdownlint.customRules": [
"./.markdownlint/no-directional-quotation-marks",
"./.markdownlint/fix-migrated-links"
"./.markdownlint/fix-migrated-links",
"./.markdownlint/find-obsolete-links"
],
"stylelint.packageManager": "yarn"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"wikimedia-sync": "scripts/wikimedia-sync.js",
"component-spellings": "scripts/component-helper.js spelling",
"test": "jest",
"mdlint-doc": "markdownlint --rules .markdownlint/no-directional-quotation-marks --rules .markdownlint/no-wikilinks --rules .markdownlint/fix-migrated-links",
"mdlint-doc": "markdownlint --rules .markdownlint/no-directional-quotation-marks --rules .markdownlint/no-wikilinks --rules .markdownlint/fix-migrated-links --rules .markdownlint/find-obsolete-links",
"mdlint-all": "yarn mdlint-doc '{docs,general}/**/*.md' '*.md'",
"lint": "yarn mdlint-all",
"prepare": "husky install",
Expand Down

0 comments on commit e8b5a32

Please sign in to comment.