-
Notifications
You must be signed in to change notification settings - Fork 52
Add plugins #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
michaelmccord
wants to merge
5
commits into
leebyron:main
Choose a base branch
from
michaelmccord:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add plugins #30
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
884f355
Plugins
a80ffb3
feat: make the markdown file first arg of plugins
e89545e
fix: change plugin args to use optional, variadic, positional arg
8fcb5bb
fix: ensure `markdown` is referenced from the `argv` obj
9aff2f8
feat: add subdir information to all nodes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ out | |
| node_modules | ||
| npm-debug.log | ||
| src/grammar.js | ||
| .vscode | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,61 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| var fs = require('fs'); | ||
| var path = require('path'); | ||
| var specmd = require('../'); | ||
| var yargs = require('yargs'); | ||
|
|
||
| var filepath; | ||
| var metadata; | ||
| for (var i = 2; i < process.argv.length; i++) { | ||
| if (process.argv[i] === '-m' || process.argv[i] === '--metadata') { | ||
| metadata = process.argv[++i]; | ||
| } else if (process.argv[i][0] === '-') { | ||
| errorExit('Unknown argument: ' + process.argv[i]); | ||
| } else if (!filepath) { | ||
| filepath = process.argv[i]; | ||
| } else { | ||
| errorExit('Must provide only one markdown file.'); | ||
| } | ||
| } | ||
|
|
||
| if (!filepath) { | ||
| errorExit( | ||
| 'Usage: spec-md initial-file.md\n\n' + | ||
| 'Options:\n' + | ||
| ' -m, --metadata filepath to json metadata' | ||
| ); | ||
| } | ||
|
|
||
| var options = metadata ? require(path.resolve(process.cwd(), metadata)) : {}; | ||
| var absPath = path.resolve(process.cwd(), filepath); | ||
| yargs.strict() | ||
| .config('config') | ||
| .option('metadata', { | ||
| alias: 'm', | ||
| describe: 'Additional metadata to be used in construction of the output', | ||
| string: true, | ||
| global: true, | ||
| coerce: arg=>require(path.resolve(process.cwd(), path.normalize(arg))), | ||
| }) | ||
| .command(['$0 <markdown>','html <markdown>'], 'Outputs HTML5', {}, | ||
| (argv)=>{ | ||
| return specmd.html(argv.markdown,argv.metadata ? argv.metadata : {}) | ||
| .then(_=>process.stdout.write(_)) | ||
| .catch(errorExit); | ||
| } | ||
| ) | ||
| .command(['plugin <package> <markdown> [args..]'], 'Outputs in the format of the specified plugin', | ||
| (yargs)=>{}, | ||
| (argv)=>{ | ||
| return argv.package([argv.markdown, ...argv.args], | ||
| specmd.parse(argv.markdown), argv.metadata ? argv.metadata : {}) | ||
| .then(_=>process.stdout.write(_)) | ||
| .catch(errorExit); | ||
| }) | ||
| .coerce('markdown', (arg)=>path.resolve(process.cwd(), path.normalize(arg))) | ||
| .coerce('package', (arg)=>{ | ||
| console.error(`Loading ${arg} ...`); | ||
| let package = null; | ||
| let plugin = null; | ||
| try { | ||
| package = require.resolve(arg, { | ||
| paths: [process.cwd()] | ||
| }); | ||
| plugin = require(package); | ||
| } | ||
| catch(error) { | ||
| console.error(`Could not load plugin (${arg}).`); | ||
| console.error('If the plugin is installed globally, is /usr/lib/node_modules in your NODE_PATH variable (%AppData%\\npm\\node_modules on windows)?'); | ||
| errorExit(error); | ||
| } | ||
|
|
||
| specmd.html(absPath, options).then(function (html) { | ||
| process.stdout.write(html); | ||
| }).catch(function (error) { | ||
| errorExit(error.line ? error.message : (error.stack || error)); | ||
| }); | ||
| if(typeof plugin !== 'function') { | ||
| errorExit(`The specified package (${package}) does not appear to be a plugin...`); | ||
| } | ||
| return plugin; | ||
| }) | ||
| .help() | ||
| .parse(); | ||
|
|
||
| function errorExit(msg) { | ||
| process.stderr.write(msg + '\n'); | ||
| function errorExit(error) { | ||
| error.line ? error.message : (error.stack || error) | ||
| process.stderr.write(error + '\n'); | ||
| process.exit(1); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,7 @@ var terser = require('terser'); | |
| var visit = require('./visit'); | ||
|
|
||
| function print(ast, _options) { | ||
| var options = {}; | ||
| options.highlight = _options && _options.highlight || highlight; | ||
| options.biblio = _options && _options.biblio && buildBiblio(_options.biblio) || {}; | ||
| options.head = _options && _options.head || ''; | ||
| validateSecIDs(ast, options); | ||
| assignExampleNumbers(ast, options); | ||
| assignBiblioIDs(ast, options); | ||
| var options = updateBiblio(ast, _options); | ||
| return ( | ||
| '<!DOCTYPE html>\n' + | ||
| '<!-- Built with spec-md https://spec-md.com -->\n' + | ||
|
|
@@ -23,7 +17,27 @@ function print(ast, _options) { | |
| ); | ||
| }; | ||
|
|
||
| module.exports = print; | ||
| function updateBiblio(ast, _options) { | ||
| var options = {}; | ||
| options.highlight = _options && _options.highlight || highlight; | ||
| options.biblio = _options && _options.biblio && buildBiblio(_options.biblio) || {}; | ||
| options.head = _options && _options.head || ''; | ||
| validateSecIDs(ast, options); | ||
| assignExampleNumbers(ast, options); | ||
| assignBiblioIDs(ast, options); | ||
| return options; | ||
| } | ||
|
|
||
| module.exports = { | ||
| print, | ||
| formatText, | ||
| escape, | ||
| escapeCode, | ||
| updateBiblio, | ||
| getTerms, | ||
| anchorize, | ||
| join | ||
|
Comment on lines
+33
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are implementation details that might change in the future - I'd suggest avoiding exporting them and directly relying on them |
||
| }; | ||
|
|
||
| function highlight(code, lang) { | ||
| var prismLang = getPrismLanguage(lang); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please remove yargs and use the existing simplified argument parsing? My attempt to keep dependencies low and simple