html2json

我们通常如何在数据库中存储需要显示给用户并具有某些标记属性的内容?至少造过一次博客,habr或类似博客的人会立即说-html。如果我告诉过你可以做些什么呢?JSON。



我想考虑这种存储的优缺点,或者至少尝试一下。



模因



什么?杰森 为什么还要这样做?



描述:



我们有一项服务,我们在数据库中存储了HTML以显示某些内容。



, ( ) . \

( , )



:



, , css - .



- UI, , html css .



, , , . , , , . (, )



: , , :



大文章



css javascript ( ) .



:



html, json, , .



, :



. ( ) json html. json' html, Web View .





, api ( ) \

: , , .



html, json.



: : https://habr.com/ru/company/tuturu/blog/526710/



<img src="https://habrastorage.org/webt/nz/2t/p3/nz2tp3ywl9fdr7quadxm1dzmhdo.jpeg"><br>
<sup>  , , .    . </sup><br>


{
    "type": "image",
    "src": "https://habrastorage.org/webt/nz/2t/p3/nz2tp3ywl9fdr7quadxm1dzmhdo.jpeg",
    "caption": " , , .    ."
}




: : https://habr.com/ru/post/526676/



<img src="https://habrastorage.org/webt/lr/ql/v9/lrqlv9u6cxmuoyn-0hgrik3z0aw.jpeg"><br>
<i>          .</i><br>


{
    "type": "image",
    "src": "https://habrastorage.org/webt/lr/ql/v9/lrqlv9u6cxmuoyn-0hgrik3z0aw.jpeg",
    "caption": "          ."
}


, , figcaption, , , , , .



.





( , html5 details):



<div class="spoiler" role="button" tabindex="0">
    <b class="spoiler_title">    .   </b>
    <div class="spoiler_text">,     :<br />
        <br />
        <ul>
            <li>  (  )  </li>
            <li>   </li>
            <li>  </li>
            <li>   </li>
            <li> </li>
            <li></li>
        </ul><br />
                 «  »  « 
         ».  -  ,     .<br />
        <br />
                 <a
            href="https://anabin.kmk.org/anabin.html" rel="nofollow">Anabin</a>.<br />
        <br />
          :<br />
        <br />
        <ul>
            <li>    </li>
            <li>      .      «»  
                  </li>
            <li>  </li>
            <li>   ,       ,     
            </li>
            <li>  <a
                    href="https://germania.diplo.de/ru-ru/service/05-VisaEinreise/langfristigerAufenthalt/-/1611410?openAccordionId=item-1611410-3-panel"
                    rel="nofollow"> </a>    </li>
            <li>      </li>
            <li>  </li>
        </ul><br />
        ,       .<br />
        <br />
         :      <a href="https://germania.diplo.de/ru-ru"
            rel="nofollow">   </a>   .  : ,
              .  ,     
          ,      . ,       
            «blue card»,     -  —   
          .<br />
        <br />
              ,   .<br />
        <br />
        ,  - -            3   6-.
          ,  !<br />
        <br />
        ,   . <br />
    </div>
</div>


Eeeeee ...让我们将其转换为json:



{
    "type": "details",
    "title": "    .   ",
    "child": {
        "type": "div",
        "children": [
            {
                "type": "tp",
                "text": ",     :"
            },
            {
                "type": "unordered_list",
                "children": [
                    {
                        "type": "tp",
                        "text": "  (  )  "
                    },
                    {
                        "type": "tp",
                        "text": "   "
                    },
                    {
                        "type": "tp",
                        "text": "  "
                    },
                    {
                        "type": "tp",
                        "text": "   "
                    },
                    {
                        "type": "tp",
                        "text": " "
                    },
                    {
                        "type": "tp",
                        "text": ""
                    }
                ]
            },
            {
                "type": "tp",
                "text": "         «  »  «   ».  -  ,     ."
            },
            {
                "type": "paragraph",
                "children": [
                    {
                        "type": "span",
                        "text": "        ",
                        "mode": []
                    },
                    {
                        "type": "link_span",
                        "text": "Anabin",
                        "src": "https://anabin.kmk.org/anabin.html"
                    },
                    {
                        "type": "span",
                        "text": ".",
                        "mode": []
                    }
                ]
            },
            {
                "type": "tp",
                "text": "  :"
            },
            {
                "type": "unordered_list",
                "children": [
                    {
                        "type": "tp",
                        "text": "    "
                    },
                    {
                        "type": "tp",
                        "text": "      .      «»     "
                    },
                    {
                        "type": "tp",
                        "text": "  "
                    },
                    {
                        "type": "tp",
                        "text": "   ,       ,     "
                    },
                    {
                        "type": "paragraph",
                        "children": [
                            {
                                "type": "span",
                                "text": "",
                                "mode": []
                            },
                            {
                                "type": "link_span",
                                "text": " ",
                                "src": "https://germania.diplo.de/ru-ru/service/05-VisaEinreise/langfristigerAufenthalt/-/1611410?openAccordionId=item-1611410-3-panel"
                            },
                            {
                                "type": "span",
                                "text": "   ",
                                "mode": []
                            }
                        ]
                    },
                    {
                        "type": "tp",
                        "text": "      "
                    },
                    {
                        "type": "tp",
                        "text": "  "
                    }
                ]
            },
            {
                "type": "tp",
                "text": ",       ."
            },
            {
                "type": "paragraph",
                "children": [
                    {
                        "type": "span",
                        "text": " :     ",
                        "mode": []
                    },
                    {
                        "type": "link_span",
                        "text": "   ",
                        "src": "https://germania.diplo.de/ru-ru"
                    },
                    {
                        "type": "span",
                        "text": "  .  : ,       .  ,        ,      . ,            «blue card»,     -  —      .",
                        "mode": []
                    }
                ]
            },
            {
                "type": "tp",
                "text": "      ,   ."
            },
            {
                "type": "tp",
                "text": ",  - -            3   6-.   ,  !"
            },
            {
                "type": "tp",
                "text": ",   ."
            }
        ]
    }
}


, Html , .

:



{
    "type": "paragraph",
    "children": [
        {
            "type": "span",
            "text": "        ",
            "mode": []
        },
        {
            "type": "link_span",
            "text": "Anabin",
            "src": "https://anabin.kmk.org/anabin.html"
        },
        {
            "type": "span",
            "text": ".",
            "mode": []
        }
    ]
}


: mode , bold, italic, strike , .



, .



? , span , , , TextView, html



, , ( ). html Dart, . , , — .



.



. , html , , .





? 2 :



  1. json html json json'.
  2. .


? React, , "html_to_react" ( "html-dom-parser"), , , .



:



  • json.
  • , , :

    1. ( , html)
    2. html json
    3. json html .
  • dom , "html_to_react"


:



import Benchmark from "benchmark";
import fs from 'fs';
import htmlToDOM from 'html-dom-parser';

function parse(str) {
    return JSON.parse(str);
}

function prepareAndParse(str) {
    return htmlToDOM(JSON.parse(str)["textHtml"]);
}

const suiteName = '526574'
const defaultHtml = fs.readFileSync(`./test_data/${suiteName}/html.json`);
const jHtml = fs.readFileSync(`./test_data/${suiteName}/jhtml.json`);

const suite = new Benchmark.Suite;

suite.add('Json c html2json', () => parse(jHtml))
suite.add('Html  json', () => parse(defaultHtml))
suite.add('Html  json   html-dom-parser', () => prepareAndParse(defaultHtml))

suite.on('complete', function() {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
}).on('cycle', function(event) {
    console.log(String(event.target));
})

suite.run()




:



Json c html2json x 1,892 ops/sec ±0.79% (88 runs sampled)
Html  json x 1,917 ops/sec ±0.81% (89 runs sampled)
Html  json   html-dom-parser x 798 ops/sec ±3.04% (86 runs sampled)


  1. json' html json' .
  2. ( json, html) 2 html.


, ?



:



  1. ,



  2. html



    , , . css .



    , , ios, flutter, web - .



    WebView .







:



  1. json , html



    , :



    • ,
    • json ( — bson json), , , .


  2. json html



    , -:



    • , , .
    • , .


  3. html , html, , , ( , )



  4. , ,





, , , ?



:



  1. - ui
  2. , html. , , , .. javascript .
  3. , , , .




因此,我想说的是,我不建议立即切换到本文中介绍的方法。首先,我对后端和前端的其他开发人员的意见很感兴趣。最有可能的是,我没有考虑到细微差别,而是愿意听取并讨论批评和评论。




All Articles