|
| 1 | +import { addError } from 'markdownlint-rule-helpers' |
| 2 | +import yaml from 'js-yaml' |
| 3 | + |
| 4 | +import frontmatter from '../../../../lib/read-frontmatter.js' |
| 5 | +import { getRange } from '../helpers/utils.js' |
| 6 | + |
| 7 | +export const earlyAccessReferences = { |
| 8 | + names: ['GH035', 'early-access-references'], |
| 9 | + description: |
| 10 | + 'Files that are not early access should not reference early-access or early-access files.', |
| 11 | + tags: ['early-access'], |
| 12 | + severity: 'error', |
| 13 | + information: new URL('https://github.com/github/docs/blob/main/src/content-linter/README.md'), |
| 14 | + function: function GH035(params, onError) { |
| 15 | + const filepath = params.name |
| 16 | + // Early access content is allowed to use early access references |
| 17 | + // There are several existing allowed references to `early access` |
| 18 | + // as a GitHub feature. This rule focuses on references to early |
| 19 | + // access pages. |
| 20 | + const isEarlyAccess = filepath.includes('early-access') |
| 21 | + if (isEarlyAccess) return |
| 22 | + |
| 23 | + const earlyAccessRegex = /early-access/i |
| 24 | + const earlyAccessArticlesRegex = /-early-access-/ |
| 25 | + |
| 26 | + for (let i = 0; i < params.lines.length; i++) { |
| 27 | + const line = params.lines[i] |
| 28 | + const matches = line.match(earlyAccessRegex) |
| 29 | + if (matches && !earlyAccessArticlesRegex.test(line)) { |
| 30 | + addError( |
| 31 | + onError, |
| 32 | + i + 1, |
| 33 | + 'An early access reference appears to be used in a non-early access doc. Remove early access references or disable this rule.', |
| 34 | + line, |
| 35 | + getRange(line, matches[0]), |
| 36 | + null, // No fix possible |
| 37 | + ) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // The only Frontmatter property that is allowed to contain |
| 42 | + // early-access is the redirect_from property. |
| 43 | + // To get the list of frontmatter lines that are applicable |
| 44 | + // we can convert the frontmatter lines array to an object |
| 45 | + // remove the property then convert it back to strings. |
| 46 | + const frontmatterString = params.frontMatterLines.join('\n') |
| 47 | + const fm = frontmatter(frontmatterString).data |
| 48 | + delete fm.redirect_from |
| 49 | + // The landing page must link to early-access content so this |
| 50 | + // case is allowed. |
| 51 | + const isLandingPage = filepath === 'content/index.md' |
| 52 | + if (isLandingPage) delete fm.children |
| 53 | + const fmStrings = yaml.dump(fm).split('\n') |
| 54 | + |
| 55 | + for (let i = 0; i < fmStrings.length; i++) { |
| 56 | + const fmLine = fmStrings[i] |
| 57 | + if (earlyAccessRegex.test(fmLine) && !earlyAccessArticlesRegex.test(fmLine)) { |
| 58 | + addError( |
| 59 | + onError, |
| 60 | + 1, |
| 61 | + 'Frontmatter: An early access reference appears to be used in a non-early access doc. Remove early access references or disable this rule.', |
| 62 | + fmLine, |
| 63 | + null, |
| 64 | + null, // No fix possible |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | +} |
0 commit comments