|
| 1 | +require('tap').mochaGlobals() |
| 2 | +const assert = require('assert') |
| 3 | +const remark = require('remark') |
| 4 | +const frontmatter = require('remark-frontmatter') |
| 5 | +const extract = require('remark-extract-frontmatter') |
| 6 | +const yaml = require('yaml').parse |
| 7 | +const strip = require('strip-markdown') |
| 8 | +const vfile = require('to-vfile') |
| 9 | +const syllable = require('syllable') |
| 10 | +const fs = require('fs') |
| 11 | + |
| 12 | +const dir = './_haikus/' |
| 13 | +const blockList = ['haikus.md'] |
| 14 | + |
| 15 | +const files = fs.readdirSync(dir) |
| 16 | + |
| 17 | +files.forEach(async (file) => { |
| 18 | + if (!blockList.includes(file)) { |
| 19 | + const [text, meta] = await processMarkdown(dir + file) |
| 20 | + const lines = text.split('\n').filter((line) => line !== '') |
| 21 | + |
| 22 | + if (meta.test !== false) { |
| 23 | + validateHaiku(file, lines, meta) |
| 24 | + } |
| 25 | + } |
| 26 | +}) |
| 27 | + |
| 28 | +function processMarkdown(filename) { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + remark() |
| 31 | + .use(frontmatter) |
| 32 | + .use(extract, { yaml: yaml }) |
| 33 | + .use(strip) |
| 34 | + .process(vfile.readSync(filename), (err, file) => { |
| 35 | + if (err) { |
| 36 | + reject(err) |
| 37 | + } else { |
| 38 | + resolve([file.toString(), file.data]) |
| 39 | + } |
| 40 | + }) |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +function validateHaiku(filename, lines, meta) { |
| 45 | + describe(filename, () => { |
| 46 | + it("should have a '.md' file extension", () => { |
| 47 | + assert.ok(/\.md$/.test(filename), "extension does not match '.md'") |
| 48 | + }) |
| 49 | + |
| 50 | + describe('file metadata', () => { |
| 51 | + it("should have layout equal to 'haiku'", () => { |
| 52 | + assert.equal( |
| 53 | + meta.layout, |
| 54 | + 'haiku', |
| 55 | + "layout metadata should equal 'haiku'" |
| 56 | + ) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should have non-blank title', () => { |
| 60 | + assert.equal(typeof meta.title, 'string', 'title metadata is missing') |
| 61 | + }) |
| 62 | + |
| 63 | + it('should have non-blank author', () => { |
| 64 | + assert.equal(typeof meta.author, 'string', 'author metadata is missing') |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('haiku structure', () => { |
| 69 | + it('should have three lines', () => { |
| 70 | + assert.equal(lines.length, 3) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should have five syllables on the first line', () => { |
| 74 | + assert.equal(syllable(lines[0]), 5) |
| 75 | + }) |
| 76 | + |
| 77 | + it('should have seven syllables on the second line', () => { |
| 78 | + assert.equal(syllable(lines[1]), 7) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should have five syllables on the third line', () => { |
| 82 | + assert.equal(syllable(lines[2]), 5) |
| 83 | + }) |
| 84 | + }) |
| 85 | + }) |
| 86 | +} |
0 commit comments