|
| 1 | +const path = require('path') |
| 2 | +const walk = require('walk-sync') |
| 3 | +const { zip } = require('lodash') |
| 4 | +const readFileAsync = require('../../lib/readfile-async') |
| 5 | + |
| 6 | +const rootDir = path.join(__dirname, '../..') |
| 7 | +const contentDir = path.join(rootDir, 'content') |
| 8 | +const reusablesDir = path.join(rootDir, 'data/reusables') |
| 9 | + |
| 10 | +/* |
| 11 | +This will check if the markdown files in the content and data/reusables directorys starts with H3 headers |
| 12 | +*/ |
| 13 | + |
| 14 | +describe('Check H2 header start in markdown content and reusables files', () => { |
| 15 | + describe('must start with h2', () => { |
| 16 | + const mdWalkOptions = { |
| 17 | + globs: ['**/*.md'], |
| 18 | + ignore: ['**/README.md'], |
| 19 | + directories: false, |
| 20 | + includeBasePath: true |
| 21 | + } |
| 22 | + const re = /^#.*\n/gm |
| 23 | + const contentMarkdownAbsPaths = walk(contentDir, mdWalkOptions).sort() |
| 24 | + const contentMarkdownRelPaths = contentMarkdownAbsPaths.map(p => path.relative(rootDir, p)) |
| 25 | + const contentMarkdownTuples = zip(contentMarkdownRelPaths, contentMarkdownAbsPaths) |
| 26 | + |
| 27 | + const reusableMarkdownAbsPaths = walk(reusablesDir, mdWalkOptions).sort() |
| 28 | + const reusableMarkdownRelPaths = reusableMarkdownAbsPaths.map(p => path.relative(rootDir, p)) |
| 29 | + const reusableMarkdownTuples = zip(reusableMarkdownRelPaths, reusableMarkdownAbsPaths) |
| 30 | + |
| 31 | + test.each([...contentMarkdownTuples, ...reusableMarkdownTuples])( |
| 32 | + 'in "%s"', |
| 33 | + async (markdownRelPath, markdownAbsPath) => { |
| 34 | + const fileContents = await readFileAsync(markdownAbsPath, 'utf8') |
| 35 | + const match = fileContents.match(re) |
| 36 | + const firstHeader = (match) ? match[0].split(' ')[0] : null |
| 37 | + const errorMessage = 'Found the wrong header:' + match |
| 38 | + expect(['##', null], errorMessage).toContain(firstHeader) |
| 39 | + } |
| 40 | + ) |
| 41 | + }) |
| 42 | +}) |
0 commit comments