How to avoid item separators at linebreaks when printing lists?

Consider the following code which prints an array of items, which may not fit into a single line:

#set page(paper: "a6")

#let sep = h(1em) + sym.bullet + h(1em)

= Actual output

#let items = "Lorem ipsum dolor sit amet consectetur adipiscing elit sed do".split()
#items.join(sep)

= Desired output

#let items1 = "Lorem ipsum dolor sit".split()
#let items2 = "amet consectetur adipiscing elit".split()
#let items3 = "sed do".split()
#items1.join(sep) \
#items2.join(sep) \
#items3.join(sep) \

Is there a built-in way or an existing package that would allow achieving the “Desired output”, i.e. avoid inserting the separator at linebreaks? I’d guess that this could be done by measuring the item sizes and building lines accordingly, but I’d rather avoid rolling my own code here.

Implemented this layout myself now:

#set page(paper: "a6")

#let sep = h(1em) + sym.bullet + h(1em)

#let join-with-linebreaks(container-size, items, sep) = {
  let sep-width = measure(sep).width

  let lines = ()
  let line = ()
  let line-len = 0pt

  for it in items {
    let item-width = measure(it).width
    line-len += item-width + sep-width
    if line-len > container-size.width {
      lines.push(line.join(sep))
      line = ()
      line-len = item-width
    }
    line.push(it)
  }
  if line.len() > 0 {
    lines.push(line.join(sep))
  }

  lines.join("\n")
}


= Actual output

#let items = "Lorem ipsum dolor sit amet consectetur adipiscing elit sed do".split()
// Could also include `layout` in the function, but for my actual usecase, it makes sense to separate it.
#layout(size => {
  join-with-linebreaks(size, items, sep)
})

= Desired output

#let items1 = "Lorem ipsum dolor sit".split()
#let items2 = "amet consectetur adipiscing elit".split()
#let items3 = "sed do".split()
#items1.join(sep) \
#items2.join(sep) \
#items3.join(sep) \

Result:

Hey @wisperwind, if you believe you’ve found a solution to your request, please press the :white_check_mark: button to mark your reply as a solution, otherwise let us know if you still need help. Thanks! :slight_smile: