|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import markdownlint from 'markdownlint' |
| 3 | +import searchReplace from 'markdownlint-rule-search-replace' |
| 4 | + |
| 5 | +import { searchReplaceConfig } from '../../style/github-docs' |
| 6 | + |
| 7 | +describe('search-replace rule in frontmatter', () => { |
| 8 | + test('TODOCS placeholder in frontmatter is detected', async () => { |
| 9 | + const markdown = ['---', 'title: TODOCS', '---', '', 'Clean content.'].join('\n') |
| 10 | + |
| 11 | + const result = await markdownlint.promises.markdownlint({ |
| 12 | + frontMatter: null, |
| 13 | + strings: { markdown }, |
| 14 | + config: { |
| 15 | + 'search-replace': searchReplaceConfig['search-replace'], |
| 16 | + }, |
| 17 | + customRules: [searchReplace], |
| 18 | + }) |
| 19 | + |
| 20 | + const errors = result.markdown || [] |
| 21 | + |
| 22 | + // Should find TODOCS in frontmatter |
| 23 | + const todosErrors = errors.filter((e) => e.errorDetail && /TODOCS/.test(e.errorDetail)) |
| 24 | + expect(todosErrors.length).toBe(1) |
| 25 | + expect(todosErrors[0].lineNumber).toBe(2) // title: TODOCS |
| 26 | + }) |
| 27 | + |
| 28 | + test('multiple TODOCS in frontmatter are all detected', async () => { |
| 29 | + const markdown = [ |
| 30 | + '---', |
| 31 | + 'title: TODOCS', |
| 32 | + 'shortTitle: TODOCS', |
| 33 | + 'intro: TODOCS', |
| 34 | + '---', |
| 35 | + '', |
| 36 | + 'Content without placeholder.', |
| 37 | + ].join('\n') |
| 38 | + |
| 39 | + const result = await markdownlint.promises.markdownlint({ |
| 40 | + frontMatter: null, |
| 41 | + strings: { markdown }, |
| 42 | + config: { |
| 43 | + 'search-replace': searchReplaceConfig['search-replace'], |
| 44 | + }, |
| 45 | + customRules: [searchReplace], |
| 46 | + }) |
| 47 | + |
| 48 | + const errors = result.markdown || [] |
| 49 | + |
| 50 | + // Should find all TODOCS instances in frontmatter |
| 51 | + const todosErrors = errors.filter((e) => e.errorDetail && /TODOCS/.test(e.errorDetail)) |
| 52 | + expect(todosErrors.length).toBe(3) |
| 53 | + expect(todosErrors[0].lineNumber).toBe(2) // title: TODOCS |
| 54 | + expect(todosErrors[1].lineNumber).toBe(3) // shortTitle: TODOCS |
| 55 | + expect(todosErrors[2].lineNumber).toBe(4) // intro: TODOCS |
| 56 | + }) |
| 57 | + |
| 58 | + test('domain rules work in frontmatter', async () => { |
| 59 | + const markdown = [ |
| 60 | + '---', |
| 61 | + 'title: Check docs.github.com for info', |
| 62 | + 'shortTitle: Visit help.github.com', |
| 63 | + 'intro: See developer.github.com for API docs', |
| 64 | + '---', |
| 65 | + '', |
| 66 | + 'Content without domain references.', |
| 67 | + ].join('\n') |
| 68 | + |
| 69 | + const result = await markdownlint.promises.markdownlint({ |
| 70 | + frontMatter: null, |
| 71 | + strings: { markdown }, |
| 72 | + config: { |
| 73 | + 'search-replace': searchReplaceConfig['search-replace'], |
| 74 | + }, |
| 75 | + customRules: [searchReplace], |
| 76 | + }) |
| 77 | + |
| 78 | + const errors = result.markdown || [] |
| 79 | + |
| 80 | + // Should find domain errors in frontmatter |
| 81 | + const domainErrors = errors.filter( |
| 82 | + (e) => e.errorDetail && /docs-domain|help-domain|developer-domain/.test(e.errorDetail), |
| 83 | + ) |
| 84 | + expect(domainErrors.length).toBe(3) |
| 85 | + expect(domainErrors[0].lineNumber).toBe(2) // docs domain in title |
| 86 | + expect(domainErrors[1].lineNumber).toBe(3) // help domain in shortTitle |
| 87 | + expect(domainErrors[2].lineNumber).toBe(4) // developer domain in intro |
| 88 | + }) |
| 89 | + |
| 90 | + test('deprecated liquid syntax in frontmatter is detected', async () => { |
| 91 | + const markdown = [ |
| 92 | + '---', |
| 93 | + 'title: "{{ site.data.variables.product.product_name }}"', |
| 94 | + 'intro: "Use {{ octicon-plus An icon }} here"', |
| 95 | + '---', |
| 96 | + '', |
| 97 | + 'Clean content.', |
| 98 | + ].join('\n') |
| 99 | + |
| 100 | + const result = await markdownlint.promises.markdownlint({ |
| 101 | + frontMatter: null, |
| 102 | + strings: { markdown }, |
| 103 | + config: { |
| 104 | + 'search-replace': searchReplaceConfig['search-replace'], |
| 105 | + }, |
| 106 | + customRules: [searchReplace], |
| 107 | + }) |
| 108 | + |
| 109 | + const errors = result.markdown || [] |
| 110 | + |
| 111 | + // Should find deprecated syntax errors in frontmatter |
| 112 | + const deprecatedErrors = errors.filter( |
| 113 | + (e) => e.errorDetail && /site\.data|octicon/.test(e.errorDetail), |
| 114 | + ) |
| 115 | + expect(deprecatedErrors.length).toBe(2) |
| 116 | + expect(deprecatedErrors[0].lineNumber).toBe(2) // site.data syntax |
| 117 | + expect(deprecatedErrors[1].lineNumber).toBe(3) // octicon syntax |
| 118 | + }) |
| 119 | +}) |
0 commit comments