-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rebber] Avoid spoilers in spoilers (solves #280)
- Loading branch information
1 parent
45e124e
commit a2d85fe
Showing
8 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/rebber-plugins/dist/preprocessors/spoilerFlatten.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
|
||
var visit = require('unist-util-visit'); | ||
|
||
module.exports = function (elemNames) { | ||
return function (_, tree) { | ||
// This should contain something like ['sCustomBlockBody', 'secretCustomBlockBody'] | ||
var elemNamesBody = elemNames.map(function (v) { | ||
return v.concat('Body'); | ||
}); // Iterate over the elements to be flattened | ||
|
||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
|
||
try { | ||
for (var _iterator = elemNamesBody[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var elemNameBody = _step.value; | ||
visit(tree, elemNameBody, function (node) { | ||
// Recursive function that flattens all matching elements and keeps the others | ||
function flattenizeTree(blockTree) { | ||
return blockTree.map(function (v) { | ||
if (elemNames.includes(v.type)) { | ||
// Get sCustomBlock > sCustomBlockBody > * | ||
var newTree = v.children.filter(function (v) { | ||
return elemNamesBody.includes(v.type); | ||
}).map(function (e) { | ||
return e.children; | ||
}).flat(1); // First level of recursion: on direct descendents | ||
|
||
return flattenizeTree(newTree); | ||
} else { | ||
// Second level of recursion: on indirect descendents | ||
if (v.children) { | ||
v.children = flattenizeTree(v.children); | ||
} | ||
|
||
return v; | ||
} | ||
}).flat(1); | ||
} // First round on direct children | ||
|
||
|
||
node.children = flattenizeTree(node.children); | ||
}); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator["return"] != null) { | ||
_iterator["return"](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
}; | ||
}; |
38 changes: 38 additions & 0 deletions
38
packages/rebber-plugins/src/preprocessors/spoilerFlatten.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const visit = require('unist-util-visit') | ||
|
||
module.exports = (elemNames) => | ||
(_, tree) => { | ||
// This should contain something like ['sCustomBlockBody', 'secretCustomBlockBody'] | ||
const elemNamesBody = elemNames.map(v => v.concat('Body')) | ||
|
||
// Iterate over the elements to be flattened | ||
for (const elemNameBody of elemNamesBody) { | ||
visit(tree, elemNameBody, node => { | ||
// Recursive function that flattens all matching elements and keeps the others | ||
function flattenizeTree (blockTree) { | ||
return blockTree.map(v => { | ||
if (elemNames.includes(v.type)) { | ||
// Get sCustomBlock > sCustomBlockBody > * | ||
const newTree = v.children | ||
.filter(v => elemNamesBody.includes(v.type)) | ||
.map(e => e.children) | ||
.flat(1) | ||
|
||
// First level of recursion: on direct descendents | ||
return flattenizeTree(newTree) | ||
} else { | ||
// Second level of recursion: on indirect descendents | ||
if (v.children) { | ||
v.children = flattenizeTree(v.children) | ||
} | ||
|
||
return v | ||
} | ||
}).flat(1) | ||
} | ||
|
||
// First round on direct children | ||
node.children = flattenizeTree(node.children) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters