-
-
Save joepie91/3e7125c395444963346f3bf619c630e9 to your computer and use it in GitHub Desktop.
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
foo{{{}} | |
{{align:center}bar | |
qux {class:test1,test2}baz{/class}{/align}} |
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
// AST | |
[ { type: 'text', text: 'foo{{{}}\n{' }, | |
{ type: 'alignment', | |
alignment: 'center', | |
contents: | |
[ { type: 'text', text: 'bar\nqux ' }, | |
{ type: 'class', | |
classNames: [ 'test1', 'test2' ], | |
contents: [ { type: 'text', text: 'baz' } ] } ] }, | |
{ type: 'text', text: '}' } ] | |
// Flattened into individual renderable text elements | |
[ { type: 'text', text: 'foo{{{}}', tags: [] }, | |
{ type: 'newline' }, | |
{ type: 'text', text: '{', tags: [] }, | |
{ type: 'text', | |
text: 'bar', | |
tags: [ { type: 'alignment', alignment: 'center', contents: null } ] }, | |
{ type: 'newline' }, | |
{ type: 'text', | |
text: 'qux ', | |
tags: [ { type: 'alignment', alignment: 'center', contents: null } ] }, | |
{ type: 'text', | |
text: 'baz', | |
tags: | |
[ { type: 'alignment', alignment: 'center', contents: null }, | |
{ type: 'class', | |
classNames: [ 'test1', 'test2' ], | |
contents: null } ] }, | |
{ type: 'text', text: '}', tags: [] } ] |
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
'use strict'; | |
module.exports = function splitTextComponents(treeList) { | |
function flatten(treeList, tagStack) { | |
return treeList.reduce((all, item) => { | |
if (item.type === "text") { | |
let splitItems = item.text.split("\n").reduce((items, line) => { | |
return items.concat([{ | |
type: "text", | |
text: line, | |
tags: tagStack | |
}, { | |
type: "newline" | |
}]); | |
}, []); | |
/* Remove the last newline, because it's not delimiting anything. */ | |
splitItems.pop(); | |
return all.concat(splitItems); | |
} else { | |
return all.concat(flatten(item.contents, tagStack.concat([item]))); | |
} | |
}, []); | |
} | |
return flatten(treeList, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment