使用 Javascript 格式化纯文本的最快方法

发布于 2024-10-21 03:57:21 字数 439 浏览 3 评论 0原文

我有一个巨大的纯文本文档,大约 700kb,对于纯文本来说非常大,我需要在云上对其进行格式化,将其转换为 HTML,但我唯一需要替换的内容是,格式化为 HTML,以便可以通过浏览器中,为粗体斜体。对于纯文本的粗体,它们是这样的:

Not on bold... **bold text here** not bold here

斜体是这样的:

Not italic... *italic text* no italic

就像 StackOverflow 的格式一样,但问题是我需要让它更快,因为文本太大了......我的其中一个想法是添加页面幻灯片,所以我的脚本只需要格式化文本的某些部分,而不是全部,然后在用户更改页面后,脚本将再次被调用,但问题是我如何编写代码为了这一切?

I have a huge plain text document, about 700kb which is very big for plain texts and I need to format it on cloud converting it to HTML, but the only things that I need to replace, format to HTML so it can be displayed by the browser, are bold and italic. For bold at the plain text they are like this:

Not on bold... **bold text here** not bold here

And italic like this:

Not italic... *italic text* no italic

Just like StackOverflow does for their formatting, but the problem is that I need to make it a lot faster, since the text is so big... One of my ideas was to add a page slide, so I the script just need to format some part of the text, not it all, then after the user changes the page the script would be called again, but the problem is how I can make the code for this all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-10-28 03:57:22

您可以尝试将其分成许多 div,并尝试找到一种方法,当它们变得可见时(即用户滚动并进入内容区域)将格式应用于每个 div。

我不知道如何做到这一点,但您可能会发现 Jquery 中已经完成了类似的操作并获取一些代码。

例如这样的事情:
http://www.infinite-scroll.com/infinite-scroll-jquery-插件/

You may try to divide it in many divs and try to find a way to apply the formatting to each when they become visible (i.e. the user scrolls and they enter the content area).

I have no idea on how to do it, but you may find something similar already done in Jquery and grab some code.

Something like this for example:
http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/

执笔绘流年 2024-10-28 03:57:21

是不是

function boldAndItalicize(text) {
  return text.replace(/&/g, '&').replace(/</g, '<')
      .replace(/(\*+)([^*]{1,1024})(\*+)/g,
               function (whole, open, content, close) {
                 if (open.length === close.length) {
                   switch (open.length) {
                     case 1: return '<i>' + content + '</i>';
                     case 2: return '<b>' + content + '</b>';
                   }
                 }
                 return whole;
               });
}

太慢了?

如果您想将其拆分,您仍然需要扫描到断点,以确保不会在斜体或粗体部分内拆分。

Is

function boldAndItalicize(text) {
  return text.replace(/&/g, '&').replace(/</g, '<')
      .replace(/(\*+)([^*]{1,1024})(\*+)/g,
               function (whole, open, content, close) {
                 if (open.length === close.length) {
                   switch (open.length) {
                     case 1: return '<i>' + content + '</i>';
                     case 2: return '<b>' + content + '</b>';
                   }
                 }
                 return whole;
               });
}

too slow?

If you want to break it up, you do still need to scan to the break point, to make sure you don't split inside an italicized or bolded section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文