返回

文章代码块占比检测

前端

今天我给大家带来一个特别好玩的,在线检测文章中代码块占比的小工具。我相信很多小伙伴在写技术文章时都会遇到一个比较头疼的问题,那就是如何准确计算文章中代码块的占比。对于这个需求,我们完全可以用一个简单的在线工具来搞定!下面我就来给大家详细介绍一下这个小工具的开发过程,以及如何使用它。

首先,我们先来明确一下这个小工具的具体功能和需求。我们需要开发一个页面,用户可以将文章内容粘贴到文本框中,然后点击按钮就能得到文章中代码块的占比。这个占比应该以百分比的形式展示出来,并且页面应该尽可能简洁直观。

有了明确的需求之后,我们就开始动手开发。首先,我们创建一个简单的HTML页面,并添加一个文本框和一个按钮。文本框用于用户粘贴文章内容,按钮用于触发代码块占比的计算。

<!DOCTYPE html>
<html>
<head>
  
</head>
<body>
  <h1>文章代码块占比检测</h1>
  <p>请将文章内容粘贴到下面的文本框中:</p>
  <textarea id="article-content"></textarea>
  <button id="submit-button">计算</button>
  <p id="result"></p>
</body>
</html>

接下来,我们编写JavaScript代码来实现代码块占比的计算。我们首先需要获取用户粘贴的文本内容,然后使用正则表达式匹配出其中的代码块。代码块一般都是用反引号()或者三个反引号( )包裹起来的,所以我们可以使用如下正则表达式:

const codeBlockRegex = /`{3}(.*?)`{3}|`(.*?)`/gs;

这个正则表达式可以匹配出所有用反引号包裹起来的代码块,包括单行代码块和多行代码块。

匹配到代码块之后,我们就可以计算出代码块的总长度。然后,我们再计算出文章的总长度。最后,我们就可以通过代码块总长度除以文章总长度,得到代码块的占比。

const articleContent = document.getElementById('article-content').value;
const codeBlocks = articleContent.match(codeBlockRegex);
let codeBlockLength = 0;
if (codeBlocks) {
  codeBlocks.forEach(codeBlock => {
    codeBlockLength += codeBlock.length;
  });
}
const articleLength = articleContent.length;
const codeBlockPercentage = (codeBlockLength / articleLength) * 100;

document.getElementById('result').innerHTML = `代码块占比:${codeBlockPercentage.toFixed(2)}%`;

最后,我们只需要把计算好的代码块占比显示在页面上即可。

完整代码:

<!DOCTYPE html>
<html>
<head>
  
</head>
<body>
  <h1>文章代码块占比检测</h1>
  <p>请将文章内容粘贴到下面的文本框中:</p>
  <textarea id="article-content"></textarea>
  <button id="submit-button">计算</button>
  <p id="result"></p>

  <script>
    const articleContent = document.getElementById('article-content');
    const result = document.getElementById('result');
    const submitButton = document.getElementById('submit-button');

    submitButton.addEventListener('click', () => {
      const codeBlockRegex = /`{3}(.*?)`{3}|`(.*?)`/gs;
      const codeBlocks = articleContent.value.match(codeBlockRegex);
      let codeBlockLength = 0;
      if (codeBlocks) {
        codeBlocks.forEach(codeBlock => {
          codeBlockLength += codeBlock.length;
        });
      }
      const articleLength = articleContent.value.length;
      const codeBlockPercentage = (codeBlockLength / articleLength) * 100;

      result.innerHTML = `代码块占比:${codeBlockPercentage.toFixed(2)}%`;
    });
  </script>
</body>
</html>

这个小工具非常简单易用,只需要把文章内容粘贴到文本框中,然后点击按钮就可以得到结果。这个工具可以帮助我们快速准确地计算文章中代码块的占比,对于编写技术文章非常有帮助。

希望这个小工具能给大家带来帮助,也欢迎大家提出宝贵意见和建议!

以下是技术指南

先决条件

  • HTML
  • JavaScript
  • 正则表达式

步骤

  1. 创建一个新的HTML文件,并添加一个文本框和一个按钮。
  2. 使用正则表达式匹配出文章中的代码块。
  3. 计算代码块的总长度。
  4. 计算文章的总长度。
  5. 计算代码块的占比。
  6. 将计算好的代码块占比显示在页面上。

示例代码

<!DOCTYPE html>
<html>
<head>
  
</head>
<body>
  <h1>文章代码块占比检测</h1>
  <p>请将文章内容粘贴到下面的文本框中:</p>
  <textarea id="article-content"></textarea>
  <button id="submit-button">计算</button>
  <p id="result"></p>

  <script>
    const articleContent = document.getElementById('article-content');
    const result = document.getElementById('result');
    const submitButton = document.getElementById('submit-button');

    submitButton.addEventListener('click', () => {
      const codeBlockRegex = /`{3}(.*?)`{3}|`(.*?)`/gs;
      const codeBlocks = articleContent.value.match(codeBlockRegex);
      let codeBlockLength = 0;
      if (codeBlocks) {
        codeBlocks.forEach(codeBlock => {
          codeBlockLength += codeBlock.length;
        });
      }
      const articleLength = articleContent.value.length;
      const codeBlockPercentage = (codeBlockLength / articleLength) * 100;

      result.innerHTML = `代码块占比:${codeBlockPercentage.toFixed(2)}%`;
    });
  </script>
</body>
</html>

注意

  • 该工具只能计算代码块的长度,不能计算代码块的复杂度或质量。
  • 该工具不适用于包含大量非文本内容(如图像或表格)的文章。