经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » Markdown » 查看文章
JavaScript+Node.js写一款markdown解析器
来源:jb51  时间:2022/2/14 11:35:56  对本文有异议

1. 准备工作

首先编写getHtml函数,传入markdown文本字符串,这里使用fs读取markdown文件内容,返回值是转换过后的字符串。

  1. const fs = require('fs');
  2.  
  3. const source = fs.readFileSync('./test.md', 'utf-8');
  4.  
  5. const getHtml = (source) => {
  6. ? ? // 处理标题
  7. ? ? return source;
  8. }
  9.  
  10. const result = getHtml(source);
  11.  
  12. console.log(result);

主要设计正则表达式和String.prototype.replace方法,replace接收的第一个参数可以是正则,第二个参数如果是函数那么返回值就是所替换的内容。

2. 处理图片&超链接

图片和超链接的语法很像,![图片](url),[超链接](url),使用正则匹配同时需要排除`。props会获取正则中的$,$1,$2。也就是匹配的字符整体,第一个括号内容,第二个括号内容。比如这里props[0]就是匹配到的完整内容,第四个参数props[3]是[]中的alt,第五个参数props[4]是链接地址。

  1. const imageora = (source) => {
  2. ? ? return source.replace(/(`?)(!?)\[(.*)\]\((.+)\)/gi, (...props) => {
  3. ? ? ? ? switch (props[0].trim()[0]) {
  4. ? ? ? ? ? ? case '!': return `<a href="${props[4]}" rel="external nofollow" alt="${props[3]}">${props[3]}</a>`;
  5. ? ? ? ? ? ? case '[': return `<img src="${props[4]}" alt="${props[3]}"/>`;
  6. ? ? ? ? ? ? default: return props[0];
  7. ? ? ? ? }
  8. ? ? });
  9. }
  10.  
  11. const getHtml = (source) => {
  12. ? ? source = imageora(source);
  13. ? ? return source;
  14. }

3. 处理blockquote

这里使用\x20匹配空格。如果匹配到内容,将文本props[3]放在blockquote标签返回就行了。

  1. const block = (source) => {
  2. ? ? return source.replace(/(.*)(`?)\>\x20+(.+)/gi, (...props) => {
  3. ? ? ? ? switch (props[0].trim()[0]) {
  4. ? ? ? ? ? ? case '>': return `<blockquote>${props[3]}</blockquote>`;
  5. ? ? ? ? ? ? default: return props[0];
  6. ? ? ? ? }
  7. ? ? });
  8. }

4. 处理标题

匹配必须以#开头,并且#的数量不能超过6,因为h6是最大的了,没有h7,最后props[2]是#后跟随的文本。

  1. const formatTitle = (source) => {
  2. ? ? return source.replace(/(.*#+)\x20?(.*)/g, (...props) => {
  3. ? ? ? ? switch (props[0][0]) {
  4. ? ? ? ? ? ? case '#': if (props[1].length <= 6) {
  5. ? ? ? ? ? ? ? ? return `<h${props[1].length}>${props[2].trim()}</h${props[1].length}>`;
  6. ? ? ? ? ? ? };
  7. ? ? ? ? ? ? default: return props[0];
  8. ? ? ? ? }
  9. ? ? })
  10. }

5. 处理字体

写的开始复杂了

  1. const formatFont = (source) => {
  2. ? ? // 处理 ~ 包裹的文本
  3. ? ? source = source.replace(/([`\\]*\~{2})(.*?)\~{2}/g, (...props) => {
  4. ? ? ? ? switch (props[0].trim()[0]) {
  5. ? ? ? ? ? ? case '~': return `<del>${props[2]}</del>`;;
  6. ? ? ? ? ? ? default: return props[0];
  7. ? ? ? ? }
  8. ? ? });
  9. ? ? // 处理 * - 表示的换行
  10. ? ? source = source.replace(/([`\\]*)[* -]{3,}\n/g, (...props) => {
  11. ? ? ? ? switch (props[0].trim()[0]) {
  12. ? ? ? ? ? ? case '*': ;
  13. ? ? ? ? ? ? case '-': return `<hr />`;
  14. ? ? ? ? ? ? default: return props[0];
  15. ? ? ? ? }
  16. ? ? })
  17. ? ? // 处理***表示的加粗或者倾斜。
  18. ? ? source = source.replace(/([`\\]*\*{1,3})(.*?)(\*{1,3})/g, (...props) => {
  19. ? ? ? ? switch (props[0].trim()[0]) {
  20. ? ? ? ? ? ? case '*': if (props[1] === props[3]) {
  21. ? ? ? ? ? ? ? ? if (props[1].length === 1) {
  22. ? ? ? ? ? ? ? ? ? ? return `<em>${props[2]}</em>`;;
  23. ? ? ? ? ? ? ? ? } else if (props[1].length === 2) {
  24. ? ? ? ? ? ? ? ? ? ? return `<strong>${props[2]}</strong>`;;
  25. ? ? ? ? ? ? ? ? } else if (props[1].length === 3) {
  26. ? ? ? ? ? ? ? ? ? ? return `<strong><em>${props[2]}</em></strong>`;;
  27. ? ? ? ? ? ? ? ? }
  28. ? ? ? ? ? ? };
  29. ? ? ? ? ? ? default: return props[0];
  30. ? ? ? ? }
  31. ? ? });
  32. ? ? return source;
  33. }

6. 处理代码块

使用正则匹配使用`包裹的代码块,props[1]是开头`的数量,props[5]是结尾`的数量,必须相等才生效。

  1. const pre = (source) => {
  2. ? ? source = source.replace(/([\\`]+)(\w+(\n))?([^!`]*?)(`+)/g, (...props) => {
  3. ? ? ? ? switch (props[0].trim()[0]) {
  4. ? ? ? ? ? ? case '`': if (props[1] === props[5]) {
  5. ? ? ? ? ? ? ? ? return `<pre>${props[3] || ''}${props[4]}</pre>`;
  6. ? ? ? ? ? ? };
  7. ? ? ? ? ? ? default: return props[0];
  8. ? ? ? ? }
  9. ? ? });
  10. ? ? return source;
  11. }

7. 处理列表

这里只是处理了ul无序列表,写的同样很麻烦。主要我的思路是真复杂。而且bug肯定也不少。先匹配-+*加上空格,然后根据这一行前面的空格熟替换为ul。这样每一行都保证被ulli包裹。

第二步判断相邻ul之间相差的个数,如果相等则表示应该是同一个ul的li,替换掉</ul><ul>为空,如果后一个ul大于前一个ul,则表示后面有退格,新生成一个<ul>包裹退格后的li,如果是最后一个ul则补齐前面所有的</ul>。

  1. const list = (source) => {
  2. ? ? source = source.replace(/.*?[\x20\t]*([\-\+\*]{1})\x20(.*)/g, (...props) => {
  3. ? ? ? ? if (/^[\t\x20\-\+\*]/.test(props[0])) {
  4. ? ? ? ? ? ? return props[0].replace(/([\t\x20]*)[\-\+\*]\x20(.*)/g, (...props) => {
  5. ? ? ? ? ? ? ? ? const len = props[1].length || '';
  6. ? ? ? ? ? ? ? ? return `<ul${len}><li>${props[2]}</li></ul${len}>`;
  7. ? ? ? ? ? ? })
  8. ? ? ? ? } else {
  9. ? ? ? ? ? ? return props[0];
  10. ? ? ? ? }
  11. ? ? });
  12. ? ? const set = new Set();
  13. ? ? source = source.replace(/<\/ul(\d*)>(\n<ul(\d*)>)?/g, (...props) => {
  14. ? ? ? ? set.add(props[1]);
  15. ? ? ? ? if (props[1] == props[3]) {
  16. ? ? ? ? ? ? return '';
  17. ? ? ? ? } else if (props[1] < props[3]) {
  18. ? ? ? ? ? ? return '<ul>';
  19. ? ? ? ? } else {
  20. ? ? ? ? ? ? const arr = [...set];
  21. ? ? ? ? ? ? const end = arr.indexOf(props[1]);
  22. ? ? ? ? ? ? let start = arr.indexOf(props[3]);
  23. ? ? ? ? ? ? if (start > 0) {
  24. ? ? ? ? ? ? ? ? return '</ul>'.repeat(end - start);
  25. ? ? ? ? ? ? } else {
  26. ? ? ? ? ? ? ? ? return '</ul>'.repeat(end + 1);
  27. ? ? ? ? ? ? } ? ? ? ? ? ?
  28. ? ? ? ? }
  29. ? ? });
  30. ? ? return source.replace(/<(\/?)ul(\d*)>/g, '<$1ul>');
  31. }

8. 处理表格

  1. const table = (source) => {
  2. ? ? source = source.replace(/\|.*\|\n\|\s*-+\s*\|.*\|\n/g, (...props) => {
  3. ? ? ? ? let str = '<table><tr>';
  4. ? ? ? ? const data = props[0].split(/\n/)[0].split('|');
  5. ? ? ? ? for (let i = 1; i < data.length - 1; i++) {
  6. ? ? ? ? ? ? str += `<th>${data[i].trim()}</th>`
  7. ? ? ? ? }
  8. ? ? ? ? str += '<tr></table>';
  9. ? ? ? ? return str;
  10. ? ? });
  11. ? ? return formatTd(source);
  12. }
  13.  
  14. const formatTd = (source) => {
  15. ? ? source = source.replace(/<\/table>\|.*\|\n/g, (...props) => {
  16. ? ? ? ? let str = '<tr>';
  17. ? ? ? ? const data = props[0].split('|');
  18. ? ? ? ? for (let i = 1; i < data.length - 1; i++) {
  19. ? ? ? ? ? ? str += `<td>${data[i].trim()}</td>`
  20. ? ? ? ? }
  21. ? ? ? ? str += '<tr></table>';
  22. ? ? ? ? return str;
  23. ? ? });
  24. ? ? if (source.includes('</table>|')) {
  25. ? ? ? ? return formatTd(source);
  26. ? ? }
  27. ? ? return source;
  28. }

9. 调用方法

  1. const getHtml = (source) => {
  2. ? ? source = imageora(source);
  3. ? ? source = block(source);
  4. ? ? source = formatTitle(source);
  5. ? ? source = formatFont(source);
  6. ? ? source = pre(source);
  7. ? ? source = list(source);
  8. ? ? source = table(source);
  9. ? ? return source;
  10. }
  11.  
  12. const result = getHtml(source);
  13.  
  14. console.log(result);

到此这篇关于JavaScript+Node.js写一款markdown解析器的文章就介绍到这了,更多相关写一款markdown解析器内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号