turndown icon indicating copy to clipboard operation
turndown copied to clipboard

Set line-width

Open ckoppelman opened this issue 9 years ago • 4 comments

It would be great to be able to set line-width for content. That is, if I want an 80-char line, the last whitespace before 80-chars turns into a \n. This is especially useful for <blockquote>.

ckoppelman avatar Apr 07 '16 21:04 ckoppelman

This can be done at the moment

// a general fillColumn function

const fillColumn = function (width, s) {
  if (s.length <= width) {
    return s
  } else {
    const head = s.slice(0, width)
    const idx = head.lastIndexOf(' ') + 1
    return s.slice(0, idx) + '\n' + fillColumn(width, s.slice(idx))
  }
}

const options = {
  converters: [
    {
      filter: 'blockquote',
      replacement: function (content) {
        content = content.trim()
        content = content.replace(/\n{3,}/g, '\n\n')
        content = fillColumn(78, content)
        content = content.split('\n').map(e => '> ' + e).join('\n')
        // content = content.replace(/^/gm, '> ')
        return '\n\n' + content + '\n\n'
      }
    }
  ]
}
const tag = `<blockquote>Within that group of sufferers, however, the condition varies 
widely. For the vast majority, the problem is not so much 
about detecting a face. Prosopagnosics can see eyes, noses
 and mouths as clearly as anyone else. </blockquote>`
p(toMarkdown(tag, options))

// > Within that group of sufferers, however, the condition varies widely. For the 
// > vast majority, the problem is not so much about detecting a face. 
// > Prosopagnosics can see eyes, noses and mouths as clearly as anyone else.

I would recommend not to hardcode such set line-width function in this package. A general implementation could be provided in the doc as a hint to users.

academyofzhuang avatar Aug 04 '17 07:08 academyofzhuang

One of the difficulties with setting a line-width is handling nested elements. For example, say you have some nested lists inside a blockquote. If every converter added a line break at 80 characters, then the eventual output would end up being longer than 80 characters because the list and block quote converters each add characters to the beginning of a line.

In order for this to work, a converter would need to "know" the element's context, and how far it has already been offset. It's not impossible, but would require some figuring out.

domchristie avatar Aug 04 '17 12:08 domchristie

Then it might be best to do this in a post-processing step. But that would be a super-annoying post-processing with a requirement to know how to continue each style of line.

ckoppelman avatar Aug 04 '17 15:08 ckoppelman

But that would be a super-annoying post-processing with a requirement to know how to continue each style of line.

Exactly 😫

domchristie avatar Aug 04 '17 15:08 domchristie