Skip to content

Commit

Permalink
remark-util-split: simplify the goal
Browse files Browse the repository at this point in the history
  • Loading branch information
artragis committed Mar 6, 2019
1 parent 2d76dba commit 1368878
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 90 deletions.
36 changes: 19 additions & 17 deletions packages/mdast-util-split/__tests__/index.js
Expand Up @@ -7,7 +7,6 @@ const doSplit = (text, {splitDepth = 1,
introductionAsProperty = true, conclusionAsProperty = false}) => {
return split(unified().use(reParse).parse(text), {
splitDepth: splitDepth,
introductionAsProperty: introductionAsProperty,
conclusionAsProperty: conclusionAsProperty})
}

Expand Down Expand Up @@ -46,7 +45,8 @@ test('default parameter with canonical text', () => {
],
})
expect(result.trees.length).toBe(2)
expect(result.trees[0].introduction).toMatchObject({
expect(result.conclusion).toBeFalsy()
expect(result.trees[0].children).toMatchObject({
type: 'root',
children: [
{
Expand Down Expand Up @@ -81,14 +81,6 @@ test('default parameter with canonical text', () => {
},
],
},
],
})
expect(result.trees[1].introduction).toBeFalsy()
expect(result.trees[0].conclusion).toBeFalsy()
expect(result.trees[1].conclusion).toBeFalsy()
expect(result.trees[0].children).toMatchObject({
type: 'root',
children: [
{
type: 'heading',
depth: 2,
Expand Down Expand Up @@ -126,8 +118,8 @@ test('default parameter with canonical text', () => {
})
})

test('do not split introduction with canonical text', () => {
const result = doSplit(text, {introductionAsProperty: false})
test('extract conclusion', () => {
const result = doSplit(text, {conclusionAsProperty: true})
expect(result.introduction).toMatchObject({
type: 'root',
children: [
Expand All @@ -142,11 +134,7 @@ test('do not split introduction with canonical text', () => {
},
],
})
expect(result.trees.length).toBe(2)
expect(result.trees[0].introduction).toBeFalsy()
expect(result.trees[1].introduction).toBeFalsy()
expect(result.trees[0].conclusion).toBeFalsy()
expect(result.trees[1].conclusion).toBeFalsy()
expect(result.trees.length).toBe(1)
expect(result.trees[0].children).toMatchObject({
type: 'root',
children: [
Expand Down Expand Up @@ -203,6 +191,20 @@ test('do not split introduction with canonical text', () => {
},
],
})
expect(result.conclusion).toMatchObject({
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'paragraph',
},
],
},
],
})
})

test('no heading', () => {
Expand Down
80 changes: 7 additions & 73 deletions packages/mdast-util-split/src/index.js
Expand Up @@ -5,7 +5,6 @@ const ROOT_HEADING_DEPTH = 1
module.exports = function split (tree, config) {
const {
splitDepth = ROOT_HEADING_DEPTH,
introductionAsProperty = true,
conclusionAsProperty = false,
} = config

Expand All @@ -15,40 +14,22 @@ module.exports = function split (tree, config) {
}
return splitAtDepth(
tree,
ROOT_HEADING_DEPTH,
{splitDepth, introductionAsProperty, conclusionAsProperty}
splitDepth, conclusionAsProperty
)
}

function splitAtDepth (tree, currentDepth, config) {
const {
splitDepth = ROOT_HEADING_DEPTH,
introductionAsProperty = true,
conclusionAsProperty = false,
} = config
function splitAtDepth (tree, currentDepth, conclusionAsProperty) {

const splitter = new Splitter(currentDepth)
const hasHeading = !!find(tree, {type: 'heading', depth: currentDepth})
if (!hasHeading) {
return tree
}
visit(tree, null, (node, index, parent) => splitter.visit(node, index, parent))
if (conclusionAsProperty) {
splitter.extractConclusions()
}
if (introductionAsProperty) {
splitter.extractIntroductions()
}
if (currentDepth < splitDepth) {
for (let i = 0; i < splitter.subTrees.length; i++) {
const newTree = newRootTree(splitter.subTrees[i].children.children)
const result = splitAtDepth(newTree, currentDepth + 1, config)
splitter.subTrees[i].children = result
}
let conclusion = null
if (conclusionAsProperty && splitter.subTrees.length > 1) {
conclusion = splitter.subTrees.pop().children
}
return {
introduction: splitter.introduction,
trees: splitter.subTrees,
conclusion: conclusion,
}
}

Expand All @@ -73,54 +54,7 @@ class Splitter {
this.lastIndex = -1
this.subTrees = []
this.depth = depth
this._introduction = newRootTree()
}
set introduction (nodes) {
this._introduction = newRootTree(nodes)
}
get introduction () {
return this._introduction
}
extractConclusions () {
this.subTrees.forEach(splittedPart => {
const firstHeading = find(splittedPart.children, {
type: 'heading',
depth: this.depth + 1,
})
let lastHeading = null
let lastIndex = 0
for (let i = splittedPart.children.length - 1; i > 0; i--) {
if (splittedPart.children[i] === 'heading' &&
splittedPart.children[i].depth === this.depth - 1) {
lastHeading = splittedPart.children[i]
lastIndex = i
break
}
}
if (!lastHeading || lastHeading === firstHeading) {
return
}
const rootContent = splittedPart.children.splice(
lastIndex,
splittedPart.children.length - lastIndex)
splittedPart.conclusion = newRootTree(rootContent)
})
}
extractIntroductions () {
this.subTrees.forEach(
(subTree) => {
const firstHeading = find(subTree.children, {
type: 'heading',
depth: this.depth + 1,
})
if (firstHeading) {
const rootContent = subTree.children.children.splice(
0,
subTree.children.children.indexOf(firstHeading))
subTree.introduction = newRootTree(rootContent)
}
}
)
this.introduction = newRootTree()
}

visit (node, index, parent) {
Expand Down

0 comments on commit 1368878

Please sign in to comment.