From e8b5a32b742aa23ded4a2cfa064774625993e812 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sun, 8 May 2022 20:54:18 +0800 Subject: [PATCH] [repo] Add markdownlint rule to identify obsolete pages --- .markdownlint/find-obsolete-links.js | 69 ++++++++++++++++++++++++++++ .vscode/settings.json | 3 +- package.json | 2 +- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 .markdownlint/find-obsolete-links.js diff --git a/.markdownlint/find-obsolete-links.js b/.markdownlint/find-obsolete-links.js new file mode 100644 index 0000000000..41c67814c3 --- /dev/null +++ b/.markdownlint/find-obsolete-links.js @@ -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 . + */ + +/* 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 = /(?\[.*\](?=(?\(https:\/\/docs.moodle.org\/dev\/(?[^)]*)\)|\[[^\]]*\])))/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); + }); + }, +}; diff --git a/.vscode/settings.json b/.vscode/settings.json index b24ef00610..b7865bd7db 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } diff --git a/package.json b/package.json index f9345acf10..87743f9f58 100644 --- a/package.json +++ b/package.json @@ -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",