From e8526029529d1e5147b3ef8b7ff534cddcb856fa Mon Sep 17 00:00:00 2001 From: Islam Naasani Date: Mon, 29 Jan 2024 22:30:56 +0300 Subject: [PATCH] feat: add mdx support --- doctoc.js | 16 ++++++++-------- lib/file.js | 2 +- lib/transform.js | 46 +++++++++++++++++++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/doctoc.js b/doctoc.js index 578dfa9..7cdad22 100755 --- a/doctoc.js +++ b/doctoc.js @@ -16,7 +16,7 @@ function cleanPath(path) { return homeExpanded.replace(/\s/g, '\\ '); } -function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, stdOut, updateOnly) { +function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, stdOut, updateOnly, syntax) { if (processAll) { console.log('--all flag is enabled. Including headers before the TOC location.') } @@ -24,13 +24,13 @@ function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPref if (updateOnly) { console.log('--update-only flag is enabled. Only updating files that already have a TOC.') } - + console.log('\n==================\n'); var transformed = files .map(function (x) { var content = fs.readFileSync(x.path, 'utf8') - , result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly); + , result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly, syntax); result.path = x.path; return result; }); @@ -48,7 +48,7 @@ function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPref console.log('"%s" is up to date', x.path); }); - changed.forEach(function (x) { + changed.forEach(function (x) { if (stdOut) { console.log('==================\n\n"%s" should be updated', x.path) } else { @@ -62,7 +62,7 @@ function printUsageAndExit(isErr) { var outputFunc = isErr ? console.error : console.info; - outputFunc('Usage: doctoc [mode] [--entryprefix prefix] [--notitle | --title title] [--maxlevel level] [--all] [--update-only] (where path is some path to a directory (e.g., .) or a file (e.g., README.md))'); + outputFunc('Usage: doctoc [mode] [--entryprefix prefix] [--notitle | --title title] [--maxlevel level] [--all] [--update-only] [--syntax (md|mdx)] (where path is some path to a directory (e.g., .) or a file (e.g., README.md))'); outputFunc('\nAvailable modes are:'); for (var key in modes) { outputFunc(' --%s\t%s', key, modes[key]); @@ -84,7 +84,7 @@ var mode = modes['github']; var argv = minimist(process.argv.slice(2) , { boolean: [ 'h', 'help', 'T', 'notitle', 's', 'stdout', 'all' , 'u', 'update-only'].concat(Object.keys(modes)) - , string: [ 'title', 't', 'maxlevel', 'm', 'entryprefix' ] + , string: [ 'title', 't', 'maxlevel', 'm', 'entryprefix', 'syntax' ] , unknown: function(a) { return (a[0] == '-' ? (console.error('Unknown option(s): ' + a), printUsageAndExit(true)) : true); } }); @@ -104,7 +104,7 @@ var entryPrefix = argv.entryprefix || '-'; var processAll = argv.all; var stdOut = argv.s || argv.stdout var updateOnly = argv.u || argv['update-only'] - +var syntax = argv['syntax'] || "md" var maxHeaderLevel = argv.m || argv.maxlevel; if (maxHeaderLevel && isNaN(maxHeaderLevel) || maxHeaderLevel < 0) { console.error('Max. heading level specified is not a positive number: ' + maxHeaderLevel), printUsageAndExit(true); } @@ -120,7 +120,7 @@ for (var i = 0; i < argv._.length; i++) { files = [{ path: target }]; } - transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, stdOut, updateOnly); + transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, stdOut, updateOnly, syntax); console.log('\nEverything is OK.'); } diff --git a/lib/file.js b/lib/file.js index 1479be9..47c85a8 100644 --- a/lib/file.js +++ b/lib/file.js @@ -2,7 +2,7 @@ var path = require('path') , fs = require('fs') , _ = require('underscore'); -var markdownExts = ['.md', '.markdown']; +var markdownExts = ['.md', '.markdown', '.mdx']; var ignoredDirs = ['.', '..', '.git', 'node_modules']; function separateFilesAndDirs(fileInfos) { diff --git a/lib/transform.js b/lib/transform.js index afd713c..636a19a 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -6,18 +6,29 @@ var _ = require('underscore') , getHtmlHeaders = require('./get-html-headers') , md = require('@textlint/markdown-to-ast'); -var start = '\n' + - '' - , end = '' - , skipTag = ''; +var commentTypes = { + md: { + start: "", + escapedStart: "