From c432385ff8b851cfc48c336055e8bebeb3f66f06 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 25 Apr 2019 12:48:38 +0200 Subject: [PATCH 1/7] add test --- test/fixtures/phrasing-content/input.md | 29 ++++++++++++++++++++++ test/fixtures/phrasing-content/output.json | 1 + test/index.js | 6 ++++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/phrasing-content/input.md create mode 100644 test/fixtures/phrasing-content/output.json diff --git a/test/fixtures/phrasing-content/input.md b/test/fixtures/phrasing-content/input.md new file mode 100644 index 0000000..188c648 --- /dev/null +++ b/test/fixtures/phrasing-content/input.md @@ -0,0 +1,29 @@ +# Text + +# *Emphasis* + +# **Importance** + +# ~~Strikethrough~~ + +# HTML + +# `inline.code()` + +# [^a footnote] + +# [^1] + +# ![image](image.png) + +# ![image reference][image] + +# [link](example.com) + +# ![link reference][link] + +[^1]: footnote reference + +[image]: image.png + +[link]: example.com diff --git a/test/fixtures/phrasing-content/output.json b/test/fixtures/phrasing-content/output.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/phrasing-content/output.json @@ -0,0 +1 @@ +{} diff --git a/test/index.js b/test/index.js index 56c794b..0155c38 100644 --- a/test/index.js +++ b/test/index.js @@ -44,10 +44,14 @@ test('Fixtures', function(t) { actual = toc( remark() .use(remarkAttr) - .parse(input), + .parse(input, {footnotes: true}), config ) + if (name === 'phrasing-content') { + console.dir(actual, {depth: null}) + } + t.deepEqual(actual, expected, name) }) From 4ef7096a476e7d5ae738712edd0a347cd74998c0 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 25 Apr 2019 12:54:48 +0200 Subject: [PATCH 2/7] =?UTF-8?q?fix=20variable=20name=20to=20reflect=20what?= =?UTF-8?q?=20we=E2=80=99re=20working=20with?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/contents.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/contents.js b/lib/contents.js index 8856d1e..3142971 100644 --- a/lib/contents.js +++ b/lib/contents.js @@ -42,8 +42,8 @@ function contents(map, tight) { return table } -// Insert a `node` into a `parent`. -function insert(node, parent, tight) { +// Insert a `entry` into a `parent`. +function insert(entry, parent, tight) { var children = parent.children var length = children.length var last = children[length - 1] @@ -51,7 +51,7 @@ function insert(node, parent, tight) { var index var item - if (node.depth === 1) { + if (entry.depth === 1) { item = listItem() item.children.push({ @@ -60,30 +60,30 @@ function insert(node, parent, tight) { { type: LINK, title: null, - url: '#' + node.id, - children: [{type: TEXT, value: node.value}] + url: '#' + entry.id, + children: [{type: TEXT, value: entry.value}] } ] }) children.push(item) } else if (last && last.type === LIST_ITEM) { - insert(node, last, tight) + insert(entry, last, tight) } else if (last && last.type === LIST) { - node.depth-- + entry.depth-- - insert(node, last, tight) + insert(entry, last, tight) } else if (parent.type === LIST) { item = listItem() - insert(node, item, tight) + insert(entry, item, tight) children.push(item) } else { item = list() - node.depth-- + entry.depth-- - insert(node, item, tight) + insert(entry, item, tight) children.push(item) } From eb37b5796e7b6f64678334cc029ac15bbc68c070 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 25 Apr 2019 13:38:52 +0200 Subject: [PATCH 3/7] wip --- lib/contents.js | 42 ++++++++++++++++++++++++++++++++++++++++-- lib/index.js | 2 ++ lib/search.js | 6 +++++- package.json | 1 + 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/contents.js b/lib/contents.js index 3142971..fd18615 100644 --- a/lib/contents.js +++ b/lib/contents.js @@ -1,12 +1,14 @@ 'use strict' +var extend = require('extend') + module.exports = contents var LIST = 'list' var LIST_ITEM = 'listItem' var PARAGRAPH = 'paragraph' var LINK = 'link' -var TEXT = 'text' +var LINK_REFERENCE = 'linkReference' // Transform a list of heading objects to a markdown list. function contents(map, tight) { @@ -61,7 +63,7 @@ function insert(entry, parent, tight) { type: LINK, title: null, url: '#' + entry.id, - children: [{type: TEXT, value: entry.value}] + children: all(entry.children) } ] }) @@ -129,6 +131,42 @@ function insert(entry, parent, tight) { } } +function all(children) { + var result = [] + var length = children.length + var index = -1 + var child + + while (++index < length) { + child = one(children[index]) + + if (child) { + result = result.concat(child) + } + } + + return result +} + +function one(node) { + var copy = extend({}, node) + + if (node.type === LINK || node.type === LINK_REFERENCE) { + return all(node.children) + } + + delete copy.children + delete copy.position + + copy = extend(true, {}, copy) + + if (node.children) { + copy.children = all(node.children) + } + + return copy +} + // Create a list. function list() { return {type: LIST, ordered: false, spread: false, children: []} diff --git a/lib/index.js b/lib/index.js index 6b09044..69ad017 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,6 +12,8 @@ function toc(node, options) { var result = search(node, heading, settings) var map = result.map + // console.dir(map, {depth: null}) + result.map = map.length === 0 ? null : contents(map, settings.tight) // No given heading. diff --git a/lib/search.js b/lib/search.js index 17c1b7b..3e8f30c 100644 --- a/lib/search.js +++ b/lib/search.js @@ -63,7 +63,11 @@ function search(root, expression, settings) { } if (!lookingForToc && value && child.depth <= maxDepth) { - map.push({depth: child.depth, value: value, id: slugs.slug(id || value)}) + map.push({ + depth: child.depth, + children: child.children, + id: slugs.slug(id || value) + }) } } } diff --git a/package.json b/package.json index 592523b..136db45 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "index.js" ], "dependencies": { + "extend": "^3.0.2", "github-slugger": "^1.2.1", "mdast-util-to-string": "^1.0.5", "unist-util-is": "^2.1.2", From bce47011ee93b6e33d8b08852df6a58b305c30e0 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 25 Apr 2019 13:44:55 +0200 Subject: [PATCH 4/7] Regenerate fixtures --- lib/index.js | 2 - test/fixtures/phrasing-content/output.json | 319 +++++++++++++++++- .../slug-for-images-and-links/output.json | 18 +- test/index.js | 4 - 4 files changed, 330 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index 69ad017..6b09044 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,8 +12,6 @@ function toc(node, options) { var result = search(node, heading, settings) var map = result.map - // console.dir(map, {depth: null}) - result.map = map.length === 0 ? null : contents(map, settings.tight) // No given heading. diff --git a/test/fixtures/phrasing-content/output.json b/test/fixtures/phrasing-content/output.json index 0967ef4..3a21f30 100644 --- a/test/fixtures/phrasing-content/output.json +++ b/test/fixtures/phrasing-content/output.json @@ -1 +1,318 @@ -{} +{ + "index": null, + "endIndex": null, + "map": { + "type": "list", + "ordered": false, + "spread": false, + "children": [ + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#text", + "children": [ + { + "type": "text", + "value": "Text" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#emphasis", + "children": [ + { + "type": "emphasis", + "children": [ + { + "type": "text", + "value": "Emphasis" + } + ] + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#importance", + "children": [ + { + "type": "strong", + "children": [ + { + "type": "text", + "value": "Importance" + } + ] + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#strikethrough", + "children": [ + { + "type": "delete", + "children": [ + { + "type": "text", + "value": "Strikethrough" + } + ] + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#ihtmli", + "children": [ + { + "type": "html", + "value": "" + }, + { + "type": "text", + "value": "HTML" + }, + { + "type": "html", + "value": "" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#inlinecode", + "children": [ + { + "type": "inlineCode", + "value": "inline.code()" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#a-footnote", + "children": [ + { + "type": "text", + "value": "^a footnote" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#1", + "children": [ + { + "type": "text", + "value": "^1" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#image", + "children": [ + { + "type": "image", + "title": null, + "url": "image.png", + "alt": "image" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#image-reference", + "children": [ + { + "type": "imageReference", + "identifier": "image", + "label": "image", + "referenceType": "full", + "alt": "image reference" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#link", + "children": [ + { + "type": "text", + "value": "link" + } + ] + } + ] + } + ] + }, + { + "type": "listItem", + "loose": false, + "spread": false, + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "link", + "title": null, + "url": "#link-reference", + "children": [ + { + "type": "imageReference", + "identifier": "link", + "label": "link", + "referenceType": "full", + "alt": "link reference" + } + ] + } + ] + } + ] + } + ] + } +} diff --git a/test/fixtures/slug-for-images-and-links/output.json b/test/fixtures/slug-for-images-and-links/output.json index c6a96f0..c5f1b7c 100644 --- a/test/fixtures/slug-for-images-and-links/output.json +++ b/test/fixtures/slug-for-images-and-links/output.json @@ -46,8 +46,10 @@ "url": "#something-else", "children": [ { - "type": "text", - "value": "Something else" + "type": "image", + "title": null, + "url": "an-image.svg", + "alt": "Something else" } ] } @@ -69,8 +71,10 @@ "url": "#something-elsefi", "children": [ { - "type": "text", - "value": "Something elsefi" + "type": "image", + "title": "Something elsefi", + "url": "an-alt.jpeg", + "alt": null } ] } @@ -96,8 +100,10 @@ "url": "#another-title", "children": [ { - "type": "text", - "value": "another title" + "type": "image", + "title": "a title", + "url": "a-link.png", + "alt": "Something iffi" } ] } diff --git a/test/index.js b/test/index.js index 0155c38..3f6ac89 100644 --- a/test/index.js +++ b/test/index.js @@ -48,10 +48,6 @@ test('Fixtures', function(t) { config ) - if (name === 'phrasing-content') { - console.dir(actual, {depth: null}) - } - t.deepEqual(actual, expected, name) }) From e54dc1c27d91b7b20b211d279afdcc42c35e3afc Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 25 Apr 2019 13:45:57 +0200 Subject: [PATCH 5/7] fix coverage --- lib/contents.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/contents.js b/lib/contents.js index fd18615..f63cb54 100644 --- a/lib/contents.js +++ b/lib/contents.js @@ -135,26 +135,23 @@ function all(children) { var result = [] var length = children.length var index = -1 - var child while (++index < length) { - child = one(children[index]) - - if (child) { - result = result.concat(child) - } + result = result.concat(one(children[index])) } return result } function one(node) { - var copy = extend({}, node) + var copy if (node.type === LINK || node.type === LINK_REFERENCE) { return all(node.children) } + copy = extend({}, node) + delete copy.children delete copy.position From 0cc1ad5da3f7b6a23c1265a386d773115186b542 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 25 Apr 2019 13:46:58 +0200 Subject: [PATCH 6/7] fix typo --- lib/contents.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contents.js b/lib/contents.js index f63cb54..25c16d8 100644 --- a/lib/contents.js +++ b/lib/contents.js @@ -44,7 +44,7 @@ function contents(map, tight) { return table } -// Insert a `entry` into a `parent`. +// Insert an entry into `parent`. function insert(entry, parent, tight) { var children = parent.children var length = children.length From e2988c8ce0ecd95a7fb3275851b7f17ee85e4f65 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 25 Apr 2019 14:03:48 +0200 Subject: [PATCH 7/7] fix footnotes, use config for some options in tests --- package.json | 2 ++ test/fixtures/normal-attr/config.json | 3 +++ test/fixtures/phrasing-content/config.json | 5 +++++ test/fixtures/phrasing-content/input.md | 2 +- test/fixtures/phrasing-content/output.json | 18 ++++++++++++++---- test/index.js | 17 ++++++++++------- 6 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/normal-attr/config.json create mode 100644 test/fixtures/phrasing-content/config.json diff --git a/package.json b/package.json index 136db45..b0ce5ae 100644 --- a/package.json +++ b/package.json @@ -33,10 +33,12 @@ "remark": "^10.0.0", "remark-attr": "^0.8.0", "remark-cli": "^6.0.0", + "remark-parse": "^6.0.3", "remark-preset-wooorm": "^4.0.0", "remark-usage": "^6.1.3", "tape": "^4.10.1", "tinyify": "^2.5.0", + "unified": "^7.1.0", "unist-builder": "^1.0.3", "xo": "^0.24.0" }, diff --git a/test/fixtures/normal-attr/config.json b/test/fixtures/normal-attr/config.json new file mode 100644 index 0000000..14e568f --- /dev/null +++ b/test/fixtures/normal-attr/config.json @@ -0,0 +1,3 @@ +{ + "useRemarkAttr": true +} diff --git a/test/fixtures/phrasing-content/config.json b/test/fixtures/phrasing-content/config.json new file mode 100644 index 0000000..be0fe09 --- /dev/null +++ b/test/fixtures/phrasing-content/config.json @@ -0,0 +1,5 @@ +{ + "remarkParseOptions": { + "footnotes": true + } +} diff --git a/test/fixtures/phrasing-content/input.md b/test/fixtures/phrasing-content/input.md index 188c648..a90fd8f 100644 --- a/test/fixtures/phrasing-content/input.md +++ b/test/fixtures/phrasing-content/input.md @@ -12,7 +12,7 @@ # [^a footnote] -# [^1] +# footnote reference [^1] # ![image](image.png) diff --git a/test/fixtures/phrasing-content/output.json b/test/fixtures/phrasing-content/output.json index 3a21f30..c1f6c9e 100644 --- a/test/fixtures/phrasing-content/output.json +++ b/test/fixtures/phrasing-content/output.json @@ -181,8 +181,13 @@ "url": "#a-footnote", "children": [ { - "type": "text", - "value": "^a footnote" + "type": "footnote", + "children": [ + { + "type": "text", + "value": "a footnote" + } + ] } ] } @@ -201,11 +206,16 @@ { "type": "link", "title": null, - "url": "#1", + "url": "#footnote-reference", "children": [ { "type": "text", - "value": "^1" + "value": "footnote reference " + }, + { + "type": "footnoteReference", + "identifier": "1", + "label": "1" } ] } diff --git a/test/index.js b/test/index.js index 3f6ac89..ce0615b 100644 --- a/test/index.js +++ b/test/index.js @@ -2,7 +2,8 @@ var test = require('tape') var fs = require('fs') var path = require('path') -var remark = require('remark') +var unified = require('unified') +var remarkParse = require('remark-parse') var remarkAttr = require('remark-attr') var u = require('unist-builder') var toc = require('..') @@ -36,17 +37,19 @@ test('Fixtures', function(t) { var config = {} var expected = JSON.parse(output) var actual + var processor = unified() try { config = JSON.parse(fs.readFileSync(join(root, name, 'config.json'))) } catch (error) {} - actual = toc( - remark() - .use(remarkAttr) - .parse(input, {footnotes: true}), - config - ) + processor.use(remarkParse, config.remarkParseOptions) + + if (config.useRemarkAttr) { + processor.use(remarkAttr) + } + + actual = toc(processor.parse(input), config) t.deepEqual(actual, expected, name) })