Skip to content

Commit

Permalink
Rewrite to use babel
Browse files Browse the repository at this point in the history
Previously, regexes were used to inspect and manipulate the example.
This commit rewrites the whole plugin to instead properly depend on
ASTs (yay!)  This also comes with the benefit of asynchronous file
system operations.  And lastly, many new tests, support for multiple
console results, better temp file management, and much better
internals.

We no longer support `options.cwd`.  Instead, set the `cwd` on the
processed file to that instead.
  • Loading branch information
wooorm committed Jun 24, 2019
1 parent 5180586 commit bd095fb
Show file tree
Hide file tree
Showing 149 changed files with 1,213 additions and 571 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
example.js
test/fixtures/fail-could-not-parse-package/index.js
2 changes: 2 additions & 0 deletions .remarkignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
example/
test/
50 changes: 30 additions & 20 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
// This section is rendered by this module from [example.js][example-js].
// > This section is rendered by this module from [`example.js`][example-js].
// > Turtles all the way down. 🐢🐢🐢

// Dependencies:
var fs = require('fs')
var remark = require('remark')
var usage = require('./index.js') // This is changed from `./index.js` to `remark-usage`
// remark-usage-ignore-next 4 - Get the files to show them in our usage example.
var readFile = require('fs').readFileSync
var join = require('path').join
var exampleJs = readFile(join('example', 'example.js'), 'utf8')
var exampleMd = readFile(join('example', 'readme.md'), 'utf8')

// Read and parse `readme.md`:
var readme = fs.readFileSync('readme.md', 'utf-8')
var ast = remark()
.use(usage)
.parse(readme)
// remark-usage-ignore-next - Use async/await so it looks nicer.
;(async function (){

// Log something with a language flag:
console.log('markdown', remark().stringify(ast.children[2]))
// Say we are making a module that exports just enough Pi (3.14159).
// We’re documenting it with a readme file, [`example/readme.md`][example-md]:
console.log('markdown', exampleMd)

// Or without language:
console.log(remark().stringify(ast.children[3]))
// …and an example script to document it [`example/example.js`][example-js-2]:
console.log('js', exampleJs)

// …If we use `remark-usage`, we can generate the `Usage` section
var path = require('path')
var vfile = require('to-vfile')
var remark = require('remark')
var usage = require('.')

var file = vfile.readSync({path: 'readme.md', cwd: 'example'})

var file = await remark()
.use(usage)
.process(file)

// Log something which is never captured:
function neverCalled() {
console.log('javascript', 'alert("test")')
}
// Now, printing `file` (the newly generated readme) yields:
console.log('markdown', String(file))

// Log something which isn’t captured because it’s not a string.
console.log(this)
// remark-usage-ignore-next
}())
5 changes: 5 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Load dependencies:
var pi = require('.')

// Logging `pi` yields:
console.log('txt', pi)
3 changes: 3 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = Math.floor(Math.PI * 1e5) / 1e5
5 changes: 5 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"private": true,
"name": "pi",
"main": "./index.js"
}
9 changes: 9 additions & 0 deletions example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PI

More than enough 🍰

## Usage

## License

MIT
Loading

0 comments on commit bd095fb

Please sign in to comment.