Git Product home page Git Product logo

Comments (4)

OliverJAsh avatar OliverJAsh commented on September 24, 2024

You can see here how we convert new line characters into HTML.

I think the fix would be a case of making the regular expression non-greedy, i.e.:

function oldConvert(html) {
  return '<p>' + html.replace(/\n([ \t]*\n)+/g, '</p><p>').replace(/\n/g, '<br>') + '</p>';
}
function newConvert(html) {
  return '<p>' + html.replace(/\n([ \t]*\n)+?/g, '</p><p>').replace(/\n/g, '<br>') + '</p>';
}

oldConvert('1\n\n2\n\n\n3');
// => "<p>1</p><p>2</p><p>3</p>"
oldConvert('1\n\n2\n\n\n\n3');
// => "<p>1</p><p>2</p><p>3</p>"
newConvert('1\n\n2\n\n\n3');
// => "<p>1</p><p>2</p><p><br>3</p>"
newConvert('1\n\n2\n\n\n\n3');
// => "<p>1</p><p>2</p><p></p><p>3</p>"

My regular expression knowledge isn’t the best, so cc'ing @theefer in on this one.

from scribe.

OliverJAsh avatar OliverJAsh commented on September 24, 2024

Bump /cc @theefer

from scribe.

theefer avatar theefer commented on September 24, 2024

The first step is to determine what the expected result should be.

1
2

3

would give

<p>1<br>2</p>
<p>3</p>

as now.

1
2


3

would give

<p>1<br>2</p>
<p><br></p>
<p>3</p>

?

And

1
2



3

would give

<p>1<br>2</p>
<p><br></p>
<p><br></p>
<p>3</p>

?
(note the dummy <br> to make the paragraph selectable; the paste handler may not need to care about this if a subsequent formatter normalises <p></p> to contain a <br>)

In that case, it sounds like we want to replace:

\n by <br>
\n\n by </p><p>
\n\n\n by </p><p></p><p>
\n\n\n\n by </p><p></p><p></p><p>

etc?

Can't think of an easy way to do this except using a function as replace argument:

"1\n2\n\n3\n\n\n4".replace(/\n\n+/g, function(newlines){
  return Array(newlines.length).join('</p><p>'); // fancy repeating pattern, thanks StackOverflow
}).replace(/\n/g, '<br>');
// "1<br>2</p><p>3</p><p></p><p>4"

from scribe.

theefer avatar theefer commented on September 24, 2024

Note that this does not correctly work if there is whitespace on the empty lines, something the original pattern accounted for. My solution could be extended to work with that somehow I'm sure.

from scribe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.