From a0b7fc360334f3ce2df75896c39645dd7a8e3820 Mon Sep 17 00:00:00 2001 From: Derek Nguyen Date: Thu, 25 Apr 2019 16:22:55 +0900 Subject: [PATCH 1/2] test --- test/index.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/test/index.js b/test/index.js index 56c794b..5e39b39 100644 --- a/test/index.js +++ b/test/index.js @@ -115,3 +115,67 @@ test('processing nodes', function(t) { t.end() }) + +test('processing html node', function(t) { + var htmlNode = u('root', [ + u('heading', {depth: 1}, [ + u('text', {value: 'Hello '}), + u('html', {value: 'World'}) + ]) + ]) + + var htmlNestedNode = u('root', [ + u('heading', {depth: 1}, [ + u('text', {value: 'Hello '}), + u('paragraph', [u('html', {value: '
World
'})]) + ]) + ]) + + const expectedHtmlMap = { + type: 'list', + ordered: false, + spread: false, + children: [ + { + type: 'listItem', + loose: false, + spread: false, + children: [ + { + type: 'paragraph', + children: [ + { + type: 'link', + title: null, + url: '#hello-world', + children: [{type: 'text', value: 'Hello World'}] + } + ] + } + ] + } + ] + } + + t.deepEqual( + toc(htmlNode), + { + index: null, + endIndex: null, + map: expectedHtmlMap + }, + 'can process html nodes' + ) + + t.deepEqual( + toc(htmlNestedNode), + { + index: null, + endIndex: null, + map: expectedHtmlMap + }, + 'can process nested html nodes' + ) + + t.end() +}) From bcb0f1ea9c660dbb5a483d95140fba26b63431ec Mon Sep 17 00:00:00 2001 From: Derek Nguyen Date: Thu, 25 Apr 2019 16:23:21 +0900 Subject: [PATCH 2/2] support html node --- lib/search.js | 7 +++++++ package.json | 2 ++ 2 files changed, 9 insertions(+) diff --git a/lib/search.js b/lib/search.js index 17c1b7b..1acd84f 100644 --- a/lib/search.js +++ b/lib/search.js @@ -6,6 +6,8 @@ var toString = require('mdast-util-to-string') var visit = require('unist-util-visit') var is = require('unist-util-is') var slugs = require('github-slugger')() +var hastToString = require('hast-util-to-string') +var rehype = require('rehype') var HEADING = 'heading' @@ -43,6 +45,11 @@ function search(root, expression, settings) { return {index: headingIndex, endIndex: closingIndex, map: map} function onheading(child, index, parent) { + visit(child, 'html', function(node) { + node.type = 'text' + node.value = hastToString(rehype.parse(node.value)) + }) + var value = toString(child) var id = child.data && child.data.hProperties && child.data.hProperties.id diff --git a/package.json b/package.json index 6494b01..11d9957 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,9 @@ ], "dependencies": { "github-slugger": "^1.2.1", + "hast-util-to-string": "^1.0.1", "mdast-util-to-string": "^1.0.5", + "rehype": "^7.0.0", "unist-util-is": "^2.1.2", "unist-util-visit": "^1.1.0" },