From 9232a9962a084a2613a950ec44ecf06ca8b2e2cd Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Sat, 18 Aug 2018 15:30:27 -0300 Subject: [PATCH 1/4] Append a new line to the end of the cli.js output --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 87dd4c0..2ef6063 100755 --- a/cli.js +++ b/cli.js @@ -37,7 +37,7 @@ if (args._.length !== 1) { '', ' --no-stripHeadingTags: Do not strip extraneous HTML tags from heading', ' text before slugifying', - '', + '', ' --indent: Provide the indentation to use - defaults to \' \'', ' (to specify a tab, use the bash-escaped $\'\\t\')' ].join('\n')); @@ -74,5 +74,5 @@ input.on('error', function onErr(err) { function output(parsed) { if (args.json) return console.log(JSON.stringify(parsed.json, null, ' ')); - process.stdout.write(parsed.content); + process.stdout.write(parsed.content+'\n'); } From ec8dea29632feb27f39b6dd2a67b91ab6feafa60 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Sat, 18 Aug 2018 15:35:37 -0300 Subject: [PATCH 2/4] Added support to html formating and links on title headings ## Installation [Go to Top](#ite---integrated-toolset-environment) --> [Installation](#installation-go-to-top) --- index.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 90cb8be..1e3c01e 100644 --- a/index.js +++ b/index.js @@ -187,15 +187,39 @@ function linkify(tok, options) { opts.num = tok.seen; var text = titleize(tok.content, opts); var slug = utils.slugify(tok.content, opts); - slug = querystring.escape(slug); + + var new_text = clearToken(text, '['); + var new_slug = clearToken(slug, '#'); + new_slug = querystring.escape(new_slug); if (opts && typeof opts.linkify === 'function') { - return opts.linkify(tok, text, slug, opts); + return opts.linkify(tok, new_text, new_slug, opts); } - tok.content = utils.mdlink(text, '#' + slug); + tok.content = utils.mdlink(new_text, '#' + new_slug); } return tok; } +/** + * Removes a given token from the string returning the new value. + * + * @param {[String]} token + * @param {[String]} delimiter + * @return {[String]} cleaned token + */ + +function clearToken(token, delimiter) { + var new_token + if(token.indexOf(delimiter) != -1) { + new_token = token.substring(0, token.lastIndexOf(delimiter)).trim(); + } + else { + new_token = token + } + // console.log('Before: ' + token) + // console.log('After : ' + new_token) + return new_token +} + /** * Titleize the title part of a markdown link. * From 1edff43c9299a97815887d672e268aaafe911bed Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Sat, 18 Aug 2018 15:56:57 -0300 Subject: [PATCH 3/4] Fixed unit test `should allow a `filter` function to filter out unwanted bullets` not passing after last feature addition. --- index.js | 31 +++++++++++++++++++++++-------- test/test.js | 1 + 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 1e3c01e..fecb946 100644 --- a/index.js +++ b/index.js @@ -188,13 +188,13 @@ function linkify(tok, options) { var text = titleize(tok.content, opts); var slug = utils.slugify(tok.content, opts); - var new_text = clearToken(text, '['); - var new_slug = clearToken(slug, '#'); - new_slug = querystring.escape(new_slug); + var slug = clearToken(slug, '#'); + var text = stripMarkdownLink(text); + slug = querystring.escape(slug); if (opts && typeof opts.linkify === 'function') { - return opts.linkify(tok, new_text, new_slug, opts); + return opts.linkify(tok, text, slug, opts); } - tok.content = utils.mdlink(new_text, '#' + new_slug); + tok.content = utils.mdlink(text, '#' + slug); } return tok; } @@ -202,9 +202,9 @@ function linkify(tok, options) { /** * Removes a given token from the string returning the new value. * - * @param {[String]} token - * @param {[String]} delimiter - * @return {[String]} cleaned token + * @param {String} token + * @param {String} delimiter + * @return {String} cleaned token */ function clearToken(token, delimiter) { @@ -220,6 +220,21 @@ function clearToken(token, delimiter) { return new_token } +/** + * Regex match markdown link + * https://stackoverflow.com/questions/37462126/regex-match-markdown-link + * + * @param {String} text + * @return {String} stripped text + */ +function stripMarkdownLink(text) { + // var str = String(text).replace(/__|\*|\#|(?:\[([^\]]*)\]\([^)]*\))/gm, '$1').trim(); + var str = String(text).replace(/__|\*|\#|(?:\[([^\]]*)\]\([^)]*\))/gm, '').trim(); + // console.log('Before: ' + text) + // console.log('After : ' + str) + return str; +} + /** * Titleize the title part of a markdown link. * diff --git a/test/test.js b/test/test.js index e93e41b..9ac1525 100644 --- a/test/test.js +++ b/test/test.js @@ -128,6 +128,7 @@ describe('options: custom functions:', function() { return str.indexOf('...') === -1; } }); + // console.log('actual.content: \n' + actual.content) assert.equal(actual.content, [ '- [AAA](#aaa)', ' * [a.1](#a1)', From 1ce38b952bfc427a67ffe0c97ebc09088ffc43d3 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Sat, 18 Aug 2018 16:02:36 -0300 Subject: [PATCH 4/4] Created the unit test `should strip out links and html on headings` --- test/test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test.js b/test/test.js index 9ac1525..034d628 100644 --- a/test/test.js +++ b/test/test.js @@ -344,6 +344,11 @@ describe('toc', function() { '- [ddd](#fez-ddd)' ].join('\n')); }); + + it('should strip out links and html on headings', function() { + assert.equal(toc('## Title [Go](#ite)').content, '- [Title](#title-go)'); + assert.equal(toc('## Title [Go]').content, '- [Title [Go]](#title-go)'); + }); }); describe('toc tokens', function() {