const intoPairs = xs => xs.slice(1).map((x, i) => [xs[i], x])
const breakAt = (places, str) => intoPairs([0, ...places, str.length]).map(
([a, b]) => str.substring(a, b)
)
const breakWhere = (words, str) => breakAt(
words.reduce((a, {offset, length}) => [...a, offset, offset + length], []),
str
)
const createNodes = (links, str) => {
const sortedLinks = links.slice(0).sort(({offset: o1}, {offset: o2}) => o1 - o2)
return breakWhere(sortedLinks, str).map((s, i) => i % 2 == 0
? {data: s, type: 'text'}
: {data: s, type: 'link', path: sortedLinks[(i - 1) / 2].path}
).filter(({data}) => data.length > 0)
}
const str = "Do you have questions or comments and do you wish to contact ABC? Please visit our customer support page."
const links = [
// {offset: 0, length: 6, path: '/path/to/doYou'},
{offset: 83, length: 16, path: '/path/to/custSupport'},
{offset: 12, length: 9, path: 'path/to/questions'},
{offset: 25, length: 8, path: 'path/to/comments'},
]
console.log(createNodes(links, str))