index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict'
  2. var fs = require('fs')
  3. var path = require('path')
  4. var test = require('tap').test
  5. var mdast = require('mdast')
  6. var inject = require('../')
  7. var fixtures = path.join(__dirname, 'fixtures')
  8. var toInject = mdast.parse(fs.readFileSync(path.join(fixtures, 'inject.md'), 'utf-8'))
  9. // Check any file in fixtures/ with something.blah.md, expecting output to equal
  10. // contents of something.expected.md
  11. fs.readdirSync(fixtures)
  12. .filter(function (f) {
  13. return /\.[^.]+\.md$/.test(f) && !/expected\.md/.test(f)
  14. })
  15. .forEach(function (f) {
  16. test(f, testInputFile.bind(null, f))
  17. })
  18. function testInputFile (f, t) {
  19. var input = fs.readFileSync(path.join(fixtures, f), 'utf-8')
  20. var expectedFile = path.join(fixtures, f.replace(/[^.]*\.md/, 'expected.md'))
  21. function plugin (mdast) {
  22. return function transform (targetAst, file, next) {
  23. t.equal(inject('API', targetAst, toInject), f !== 'noop.input.md', 'returns true when heading found')
  24. next()
  25. }
  26. }
  27. mdast.use(plugin).process(input, function (err, file, content) {
  28. t.error(err)
  29. if (process.env.UPDATE) {
  30. fs.writeFileSync(expectedFile, content)
  31. }
  32. var expected = fs.readFileSync(expectedFile, 'utf-8')
  33. t.same(content, expected)
  34. t.end()
  35. })
  36. }