remark-usage
Advanced tools
Comparing version 6.0.0 to 6.0.1
330
index.js
@@ -1,25 +0,22 @@ | ||
'use strict'; | ||
'use strict' | ||
/* Dependencies. */ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var uncached = require('require-uncached'); | ||
var heading = require('mdast-util-heading-range'); | ||
var trimTrailingLines = require('trim-trailing-lines'); | ||
var unquote = require('unquote'); | ||
var cept = require('cept'); | ||
var unified = require('unified'); | ||
var markdown = require('remark-parse'); | ||
var fs = require('fs') | ||
var path = require('path') | ||
var uncached = require('require-uncached') | ||
var heading = require('mdast-util-heading-range') | ||
var trimTrailingLines = require('trim-trailing-lines') | ||
var unquote = require('unquote') | ||
var cept = require('cept') | ||
var unified = require('unified') | ||
var markdown = require('remark-parse') | ||
/* Expose `attacher`. */ | ||
module.exports = usage; | ||
module.exports = usage | ||
/* Methods. */ | ||
var exists = fs.existsSync; | ||
var read = fs.readFileSync; | ||
var write = fs.writeFileSync; | ||
var remove = fs.unlinkSync; | ||
var resolve = path.resolve; | ||
var exists = fs.existsSync | ||
var read = fs.readFileSync | ||
var write = fs.writeFileSync | ||
var remove = fs.unlinkSync | ||
var resolve = path.resolve | ||
var processor = unified().use(markdown); | ||
var processor = unified().use(markdown) | ||
@@ -33,67 +30,75 @@ /* List of locations to look for an example. */ | ||
'example.js' | ||
]; | ||
] | ||
/* Expressions. */ | ||
var EXPRESSION_LOG = /(console\.log\()(.+)(\);?)/g; | ||
var EXPRESSION_REQUIRE = /(require\()(.+)(\);?)/g; | ||
var EXPRESSION_COMMENT = /^(\s*)(\/\/)(\s*)(.+)/; | ||
var EXPRESSION_LOG = /(console\.log\()(.+)(\);?)/g | ||
var EXPRESSION_REQUIRE = /(require\()(.+)(\);?)/g | ||
var EXPRESSION_COMMENT = /^(\s*)(\/\/)(\s*)(.+)/ | ||
/* Constants. */ | ||
var DEFAULT_HEADING = 'usage'; | ||
var DEFAULT_HEADING = 'usage' | ||
/* Post-process the example document. */ | ||
function postprocess(value, logs, options) { | ||
var tokens = []; | ||
var start = 0; | ||
var match; | ||
var content; | ||
var info; | ||
var parameters; | ||
var end; | ||
var markdown; | ||
var tokens = [] | ||
var start = 0 | ||
var match | ||
var content | ||
var info | ||
var parameters | ||
var end | ||
var markdown | ||
EXPRESSION_LOG.lastIndex = 0; | ||
EXPRESSION_LOG.lastIndex = 0 | ||
match = EXPRESSION_LOG.exec(value) | ||
while (match = EXPRESSION_LOG.exec(value)) { | ||
end = EXPRESSION_LOG.lastIndex; | ||
while (match) { | ||
end = EXPRESSION_LOG.lastIndex | ||
content = value.slice(start, end - match[0].length); | ||
content = value.slice(start, end - match[0].length) | ||
parameters = match[2].split(/\s*,\s*/); | ||
parameters = match[2].split(/\s*,\s*/) | ||
info = parameters[0]; | ||
info = unquote(info.trim()); | ||
info = logs[info]; | ||
info = parameters[0] | ||
info = unquote(info.trim()) | ||
info = logs[info] | ||
if (info && info.value) { | ||
tokens = tokens.concat(script(content, options), log(info)); | ||
tokens = tokens.concat(script(content, options), log(info)) | ||
} else { | ||
parameters = parameters.slice(1).join(', '); | ||
parameters = parameters.slice(1).join(', ') | ||
tokens = tokens.concat(script( | ||
content + match[1] + parameters + match[3], options | ||
)); | ||
tokens = tokens.concat( | ||
script(content + match[1] + parameters + match[3], options) | ||
) | ||
} | ||
start = end; | ||
start = end | ||
match = EXPRESSION_LOG.exec(value) | ||
} | ||
if (start < value.length) { | ||
tokens = tokens.concat(script(value.slice(start))); | ||
tokens = tokens.concat(script(value.slice(start), options)) | ||
} | ||
markdown = []; | ||
markdown = [] | ||
tokens.forEach(function (token) { | ||
var prev; | ||
var lang; | ||
tokens.forEach(each) | ||
markdown = markdown.filter(filter) | ||
return markdown | ||
function each(token) { | ||
var prev | ||
var lang | ||
if (token.type === 'markdown') { | ||
markdown = markdown.concat(parse(token.value)); | ||
markdown = markdown.concat(parse(token.value)) | ||
} else { | ||
prev = markdown[markdown.length - 1]; | ||
lang = 'lang' in token ? token.lang : token.type; | ||
prev = markdown[markdown.length - 1] | ||
lang = 'lang' in token ? token.lang : token.type | ||
if (prev && prev.type === 'code' && prev.lang === lang) { | ||
prev.value += token.value ? '\n' + token.value : ''; | ||
prev.value += token.value ? '\n' + token.value : '' | ||
} else { | ||
@@ -104,18 +109,16 @@ markdown.push({ | ||
value: token.value | ||
}); | ||
}) | ||
} | ||
} | ||
}); | ||
} | ||
markdown = markdown.filter(function (token) { | ||
function filter(token) { | ||
if ('value' in token) { | ||
token.value = trimTrailingLines(token.value); | ||
token.value = trimTrailingLines(token.value) | ||
return token.value !== ''; | ||
return token.value !== '' | ||
} | ||
return true; | ||
}); | ||
return markdown; | ||
return true | ||
} | ||
} | ||
@@ -125,54 +128,60 @@ | ||
function usage(options) { | ||
var settings = {}; | ||
var pack; | ||
var main; | ||
var example; | ||
var name; | ||
var cwd; | ||
var header; | ||
var settings = {} | ||
var pack | ||
var main | ||
var example | ||
var name | ||
var cwd | ||
var header | ||
if (!options) { | ||
options = {}; | ||
options = {} | ||
} | ||
cwd = options.cwd || process.cwd(); | ||
cwd = options.cwd || process.cwd() | ||
try { | ||
pack = require(resolve(cwd, 'package.json')); | ||
pack = require(resolve(cwd, 'package.json')) | ||
} catch (err) { | ||
pack = {}; | ||
pack = {} | ||
} | ||
name = options.name || pack.name || null; | ||
name = options.name || pack.name || null | ||
main = options.main; | ||
main = main ? require.resolve(resolve(cwd, main)) : resolve(cwd, pack.main || 'index.js'); | ||
main = options.main | ||
main = main | ||
? require.resolve(resolve(cwd, main)) | ||
: resolve(cwd, pack.main || 'index.js') | ||
example = options.example; | ||
example = options.example | ||
if (example) { | ||
example = require.resolve(resolve(cwd, example)); | ||
example = require.resolve(resolve(cwd, example)) | ||
} else { | ||
EXAMPLES.some(function (filepath) { | ||
filepath = resolve(cwd, filepath); | ||
EXAMPLES.some(some) | ||
} | ||
if (exists(filepath)) { | ||
example = require.resolve(filepath); | ||
return true; | ||
} | ||
settings.cwd = cwd | ||
settings.name = name | ||
settings.main = main | ||
settings.example = example | ||
return false; | ||
}); | ||
header = toExpression(options.heading || DEFAULT_HEADING) | ||
return transform | ||
function transform(tree) { | ||
heading(tree, header, runFactory(settings)) | ||
} | ||
settings.cwd = cwd; | ||
settings.name = name; | ||
settings.main = main; | ||
settings.example = example; | ||
function some(filepath) { | ||
filepath = resolve(cwd, filepath) | ||
header = toExpression(options.heading || DEFAULT_HEADING); | ||
if (exists(filepath)) { | ||
example = require.resolve(filepath) | ||
return true | ||
} | ||
return function (tree) { | ||
heading(tree, header, runFactory(settings)); | ||
}; | ||
return false | ||
} | ||
} | ||
@@ -182,3 +191,3 @@ | ||
function runFactory(options) { | ||
return run; | ||
return run | ||
@@ -188,51 +197,44 @@ /* Add an example section based on a valid example | ||
function run(start, nodes, end) { | ||
var logs = {}; | ||
var example; | ||
var source; | ||
var tmp; | ||
var stop; | ||
var logs = {} | ||
var example = options.example | ||
var source | ||
var tmp | ||
var stop | ||
example = options.example; | ||
if (!exists(example)) { | ||
throw new Error( | ||
'Missing example: `' + example + '`. ' + | ||
'Pass an `example` or use a file at: ' + | ||
EXAMPLES.join(', ') | ||
); | ||
'Missing example: `' + | ||
example + | ||
'`. ' + | ||
'Pass an `example` or use a file at: ' + | ||
EXAMPLES.join(', ') | ||
) | ||
} | ||
tmp = example + '-tmp'; | ||
tmp = example + '-tmp' | ||
source = preprocess(read(example, 'utf-8')); | ||
source = preprocess(read(example, 'utf-8')) | ||
write(tmp, source, 'utf-8'); | ||
write(tmp, source, 'utf-8') | ||
/* To Do: better tmp file management. */ | ||
stop = cept(console, 'log', function (id, lang, value) { | ||
if (!value) { | ||
value = lang; | ||
lang = null; | ||
} | ||
stop = cept(console, 'log', intercept) | ||
if (typeof value === 'string' && typeof id === 'string') { | ||
logs[id] = {id: id, lang: lang, value: value}; | ||
} | ||
}); | ||
try { | ||
uncached(tmp); | ||
uncached(tmp) | ||
} catch (err) { | ||
err.message = | ||
'Invalid example `' + example + '`. ' + | ||
'Invalid example `' + | ||
example + | ||
'`. ' + | ||
'Ensure example is a valid JavaScript file:\n\n' + | ||
err.message; | ||
err.message | ||
throw err; | ||
throw err | ||
} finally { | ||
stop(); | ||
stop() | ||
/* istanbul ignore next */ | ||
if (exists(tmp)) { | ||
remove(tmp); | ||
remove(tmp) | ||
} | ||
@@ -242,6 +244,14 @@ } | ||
/* Add markdown. */ | ||
return [start].concat( | ||
postprocess(source, logs, options), | ||
end | ||
); | ||
return [start].concat(postprocess(source, logs, options), end) | ||
function intercept(id, lang, value) { | ||
if (!value) { | ||
value = lang | ||
lang = null | ||
} | ||
if (typeof value === 'string' && typeof id === 'string') { | ||
logs[id] = {id: id, lang: lang, value: value} | ||
} | ||
} | ||
} | ||
@@ -254,3 +264,3 @@ } | ||
function script(source, options) { | ||
var tokens; | ||
var tokens | ||
@@ -265,29 +275,33 @@ /* Make sure the require to the main module | ||
* as `./module`, it is replaced with `test`. */ | ||
source = source.replace(EXPRESSION_REQUIRE, function ($0, $1, $2, $3) { | ||
var filepath = resolve(options.example, '../', unquote($2)); | ||
var quote; | ||
source = source.replace(EXPRESSION_REQUIRE, replace) | ||
/* Transform comments into markdown: */ | ||
tokens = [] | ||
source.split('\n').forEach(each) | ||
return tokens | ||
function replace($0, $1, $2, $3) { | ||
var filepath = resolve(options.example, '../', unquote($2)) | ||
var quote | ||
if (options.main === filepath && options.name) { | ||
/* Honour quote style. */ | ||
quote = $2.charAt(0); | ||
quote = $2.charAt(0) | ||
return $1 + quote + options.name + quote + $3; | ||
return $1 + quote + options.name + quote + $3 | ||
} | ||
return $0; | ||
}); | ||
return $0 | ||
} | ||
/* Transform comments into markdown: */ | ||
tokens = []; | ||
function each(line) { | ||
var match = line.match(EXPRESSION_COMMENT) | ||
source.split('\n').forEach(function (line) { | ||
var match = line.match(EXPRESSION_COMMENT); | ||
tokens.push({ | ||
type: match ? 'markdown' : 'javascript', | ||
value: match ? match[4] : line | ||
}); | ||
}); | ||
return tokens; | ||
}) | ||
} | ||
} | ||
@@ -297,10 +311,10 @@ | ||
function preprocess(value) { | ||
var index = 0; | ||
var index = 0 | ||
value = value.replace(EXPRESSION_LOG, function ($0, $1, $2, $3) { | ||
index++; | ||
return $1 + '"remark-usage-' + index + '",' + $2 + $3; | ||
}); | ||
return value.replace(EXPRESSION_LOG, replace) | ||
return value; | ||
function replace($0, $1, $2, $3) { | ||
index++ | ||
return $1 + '"remark-usage-' + index + '",' + $2 + $3 | ||
} | ||
} | ||
@@ -311,3 +325,3 @@ | ||
function parse(value) { | ||
return processor.parse(value, {position: false}).children; | ||
return processor.parse(value, {position: false}).children | ||
} | ||
@@ -317,3 +331,3 @@ | ||
function log(info) { | ||
return {type: 'code', lang: info.lang, value: info.value}; | ||
return {type: 'code', lang: info.lang, value: info.value} | ||
} | ||
@@ -323,3 +337,3 @@ | ||
function toExpression(value) { | ||
return new RegExp('^(' + value + ')$', 'i'); | ||
return new RegExp('^(' + value + ')$', 'i') | ||
} |
{ | ||
"name": "remark-usage", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "Add a usage example to your README", | ||
@@ -12,4 +12,4 @@ "license": "MIT", | ||
], | ||
"repository": "https://github.com/wooorm/remark-usage", | ||
"bugs": "https://github.com/wooorm/remark-usage/issues", | ||
"repository": "remarkjs/remark-usage", | ||
"bugs": "https://github.com/remarkjs/remark-usage/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
@@ -25,6 +25,6 @@ "contributors": [ | ||
"mdast-util-heading-range": "^2.0.0", | ||
"remark-parse": "^4.0.0", | ||
"remark-parse": "^5.0.0", | ||
"require-uncached": "^1.0.2", | ||
"trim-trailing-lines": "^1.0.0", | ||
"unified": "^6.0.0", | ||
"unified": "^7.0.0", | ||
"unquote": "^1.1.0" | ||
@@ -35,16 +35,15 @@ }, | ||
"negate": "^1.0.0", | ||
"nyc": "^11.0.0", | ||
"remark": "^8.0.0", | ||
"remark-cli": "^4.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"nyc": "^12.0.0", | ||
"prettier": "^1.14.2", | ||
"remark": "^9.0.0", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.0.0", | ||
"xo": "^0.18.0" | ||
"xo": "^0.22.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark *.md -qfo", | ||
"build": "npm run build-md", | ||
"lint": "xo", | ||
"format": "remark *.md -qfo && prettier --write *.js test/*.js && xo *.js test/*.js --fix", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test/index.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run test-coverage" | ||
}, | ||
@@ -57,9 +56,21 @@ "nyc": { | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"max-lines": "off", | ||
"no-cond-assign": "off", | ||
"import/no-dynamic-require": "off" | ||
"no-unused-expressions": "off", | ||
"no-unused-vars": "off", | ||
"eol-last": "off", | ||
"import/no-dynamic-require": "off", | ||
"import/order": "off", | ||
"unicorn/import-index": "off", | ||
"ava/no-import-test-files": "off" | ||
}, | ||
@@ -66,0 +77,0 @@ "ignore": [ |
@@ -20,5 +20,5 @@ # remark-usage [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat] | ||
```javascript | ||
var fs = require('fs'); | ||
var remark = require('remark'); | ||
var usage = require('remark-usage'); // This is changed from `./index.js` to `remark-usage` | ||
var fs = require('fs') | ||
var remark = require('remark') | ||
var usage = require('remark-usage') // This is changed from `./index.js` to `remark-usage` | ||
``` | ||
@@ -29,4 +29,6 @@ | ||
```javascript | ||
var readme = fs.readFileSync('readme.md', 'utf-8'); | ||
var ast = remark().use(usage).parse(readme); | ||
var readme = fs.readFileSync('readme.md', 'utf-8') | ||
var ast = remark() | ||
.use(usage) | ||
.parse(readme) | ||
``` | ||
@@ -50,3 +52,3 @@ | ||
function neverCalled() { | ||
console.log('javascript', 'alert("test")'); | ||
console.log('javascript', 'alert("test")') | ||
} | ||
@@ -58,3 +60,3 @@ ``` | ||
```javascript | ||
console.log(this); | ||
console.log(this) | ||
``` | ||
@@ -114,2 +116,10 @@ | ||
## Contribute | ||
See [`contributing.md` in `remarkjs/remark`][contributing] for ways to get | ||
started. | ||
This organisation has a [Code of Conduct][coc]. By interacting with this | ||
repository, organisation, or community you agree to abide by its terms. | ||
## License | ||
@@ -121,13 +131,13 @@ | ||
[build-badge]: https://img.shields.io/travis/wooorm/remark-usage.svg | ||
[build-badge]: https://img.shields.io/travis/remarkjs/remark-usage.svg | ||
[build-status]: https://travis-ci.org/wooorm/remark-usage | ||
[build-status]: https://travis-ci.org/remarkjs/remark-usage | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/remark-usage.svg | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark-usage.svg | ||
[coverage-status]: https://codecov.io/github/wooorm/remark-usage | ||
[coverage-status]: https://codecov.io/github/remarkjs/remark-usage | ||
[chat-badge]: https://img.shields.io/gitter/room/wooorm/remark.svg | ||
[chat-badge]: https://img.shields.io/gitter/room/remarkjs/Lobby.svg | ||
[chat]: https://gitter.im/wooorm/remark | ||
[chat]: https://gitter.im/remarkjs/Lobby | ||
@@ -140,3 +150,3 @@ [license]: LICENSE | ||
[remark]: https://github.com/wooorm/remark | ||
[remark]: https://github.com/remarkjs/remark | ||
@@ -146,1 +156,5 @@ [usage]: #usage | ||
[example-js]: example.js | ||
[contributing]: https://github.com/remarkjs/remark/blob/master/contributing.md | ||
[coc]: https://github.com/remarkjs/remark/blob/master/code-of-conduct.md |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
13858
255
153
9
1
+ Added@types/node@22.9.0(transitive)
+ Added@types/unist@2.0.113.0.3(transitive)
+ Added@types/vfile@3.0.2(transitive)
+ Added@types/vfile-message@2.0.0(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedremark-parse@5.0.0(transitive)
+ Addedundici-types@6.19.8(transitive)
+ Addedunified@7.1.0(transitive)
+ Addedunist-util-stringify-position@4.0.0(transitive)
+ Addedvfile@3.0.1(transitive)
+ Addedvfile-message@4.0.2(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedremark-parse@4.0.0(transitive)
- Removedunified@6.2.0(transitive)
- Removedvfile@2.3.0(transitive)
Updatedremark-parse@^5.0.0
Updatedunified@^7.0.0