简体   繁体   English

有效地将换行符转换为段落

[英]Converting line breaks to paragraphs efficiently

I'm trying to find a fail proof to convert the bad practice of using double line breaks into proper paragraphs. 我试图找到一种失败证明,以将使用双换行符的不良做法转换为适当的段落。 Here's what I have so far. 到目前为止,这就是我所拥有的。 I know normally you wouldn't do this via JS but this is just for personal use and for the sake of learning. 我知道通常您不会通过JS进行此操作,但这只是出于个人目的,也是为了学习。

HTML: HTML:

<article>
<h1>Lorem ipsum</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum faucibus augue id viverra congue. Vestibulum mattis dui finibus pellentesque viverra. In hac habitasse platea dictumst.
<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum faucibus augue id viverra congue. Vestibulum mattis dui finibus pellentesque viverra. In hac habitasse platea dictumst.
<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum faucibus augue id viverra congue. Vestibulum mattis dui finibus pellentesque viverra. In hac habitasse platea dictumst.
<br><br>
</article>

JS: JS:

$(function() {

    // for each article, split text by <br> and wrap paragraphs in <p>
    $('article').each(function() {
        var source = $(this).html();
        var paragraphs = source.split('<br>');
        var output = '';
        for (var i = 0, l = paragraphs.length; i < l; i++) {
            output += '<p>' + paragraphs[i] + '</p>';
        }

        // Put paragraphs back together as <p>, trim white spaces and remove resulting empty <p>
        $(this)
            .html(output)
            .addClass('Paragraphed')
            .find('p')
            .filter(function() {
                return $.trim(this.innerHTML) == ''
            }).remove();

    }); // end of each .RichText

});

So far I just split the paragraphs based on <br> tags, and then put it back as paragraphs. 到目前为止,我只是基于<br>标记拆分了段落,然后将其放回段落中。 The problem is doing it this way skips paragraphs that are not completely wrapped by line breaks, in this example the first one, since its sibling is an <h1> . 问题在于这样做会跳过没有完全由换行符包裹的段落,在本例中为第一个,因为它的兄弟姐妹是<h1>

Does anyone know a better way to approach this? 有谁知道更好的方法来解决这个问题?

Here's a pen if you want to test the code live: http://codepen.io/Yogensia/pen/NjjGGR 如果您想实时测试代码,这是一支笔: http : //codepen.io/Yogensia/pen/NjjGGR

Try this: 尝试这个:

        $(function() {
    // wrap paragraphs in <p>
    $("article").each(function() {
        var source = $(this).html();
        var temp = source.split("</h1>");
        var source = temp[1];
        var paragraphs = source.split("<br><br>");
        var output = "";

        for (var i = 0, l = paragraphs.length - 1; i < l; i++) {
            output += "\n<p>" + paragraphs[i] + "</p>\n";
        }
        // add the heading
        output = temp[0] + "</h1>\n" + output;

        // display paragraphs
        $(this).html(output).addClass("Paragraphed");
    }); // end of each .RichText
});

See demo 观看演示

Initially, source holds all the HTML content. 最初, 源代码包含所有HTML内容。 Then, the script splits the content twice. 然后,脚本将内容拆分两次。 The first split occurs on the closing h1 tag and the second involves the two br tags. 第一次拆分发生在结束h1标签上,第二次拆分涉及两个br标签。 The second element of temp , ie temp[1] holds the content that should display without break tags. temp的第二个元素,即temp [1]保留应显示的内容,而不包含break标记。 Variable source then gets set to this value so that the script can use it to form paragraphs appropriately using opening and closing paragraph tags. 然后将变量设置为此值,以便脚本可以使用它来通过开始和结束段落标签适当地形成段落。

Note: When you adjust the paragraph length test by subtracting one to allow for zero-based indexing, then you only get the paragraphs you need and avoid having to remove an empty paragraph. 注意:当您通过减去1来调整段落长度测试以允许从零开始的索引编制时,您只会得到所需的段落,而不必删除空的段落。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM