如何将markdown解析成html

本文介绍了如何使用正则表达式解析Markdown文件,实现标题、粗体、斜体等元素到HTML的转换,重点讲解了h1、h2标签及引用、粗体和斜体的处理。通过实例演示了如何编写parseMarkdown函数,为Markdown内容生成HTML格式的输出。
摘要由CSDN通过智能技术生成

什么是markdown文件

随意打开任何markdown文件,你可能会看到类似如下内容:

# 标题1
## 标题2
**粗体**

我们需要将标题1标题2粗体转为<h1><h2><b>,如何做到这些呢?我们将利用正则表达式对其一一解析

正则表达式

不同的编程语言,其正则表达式可能会有一些差异。下面是用javascript编写的正则表达式:

const pattern = /Hello/
const text = 'Hello, beautiful people'
pattern.test(text) // true

如果你想学习更多的正则表达式知识,可以访问https://javascript.info

markdown编译器

该编译器,为parseMarkdown函数,它接收markdown文件内容,并将html文本返回

function parseMarkdown(markdownText) {
	const htmlText = markdownText // 对该文本做些编译相关的事

	return htmlText
}

String中的replace函数

replace函数的作用如下代码所示:

text = 'hello world and hello everyone'
regEx = /Hello/gi
console.log(text.replace(regEx, 'hi'))
// 输出: 'hi world and hi everyone'

i表示不区分大小写,g表示全局匹配,这表示它会对整个字符串进行匹配,而不是第一次匹配后便结束(若没有g的话,感兴趣的读者可以自己试试)

捕获分组(group)

我们使用()来捕获分组,捕获后,可以用$1表示它

text = 'hello world and hello everyone'
// 匹配由hello开始,任意字符结尾的字符串
regEx = /(Hello).*/i
console.log(text.replace(regEx, 'Matched Group: $1'))
// 输出: 'Matched Group: hello'

回到parseMarkdown函数

我们该如何编写该函数内容呢?需要准备如下的正则表达式

heading,如h1h2

#开始,任意字符结尾的表达式

const h1 = /^# (.*$)/gim
const h2 = /^## (.*$)/gim
const h3 = /^### (.*$)/gim

^表示该字符串的开始,m表示多行,使用.*我们可捕获任意字符(字母、数字、特殊字符等等)

Blockquote

>开始,注意前面必须使用转义字符\(因为正则表达式也有>,故需使用转义字符)

const bq = /^\> (.*$)/gim

粗体

匹配两个*号之间的内容

const bold = /\*\*(.*)\*\*/gim

斜体

匹配一个*号之间的内容

const italics = /\*(.*)\*/gim

imagelink和分行字符

// ![Alt text](url)
const image = /!\[(.*?)\]\((.*?)\)/gim
// [text](url)
const link = /\[(.*?)\]\((.*?)\)/gim
const lineBreak = /\n$/gim

串联所有的正则表达式

parseMarkdown的内容如下所示:

function parseMarkdown(markdownText) {
	const htmlText = markdownText
		.replace(/^### (.*$)/gim, '<h3>$1</h3>')
		.replace(/^## (.*$)/gim, '<h2>$1</h2>')
		.replace(/^# (.*$)/gim, '<h1>$1</h1>')
		.replace(/^\> (.*$)/gim, '<blockquote>$1</blockquote>')
		.replace(/\*\*(.*)\*\*/gim, '<b>$1</b>')
		.replace(/\*(.*)\*/gim, '<i>$1</i>')
		.replace(/!\[(.*?)\]\((.*?)\)/gim, "<img alt='$1' src='$2' />")
		.replace(/\[(.*?)\]\((.*?)\)/gim, "<a href='$2'>$1</a>")
		.replace(/\n$/gim, '<br />')

	return htmlText.trim()
}

编译如下markdown内容

const text = `
# Hello World
**This is a bold text**
`

console.log(parseMarkdown(text))
// <h1>Hello World</h1>
// <b>This is a bold text</b><br />

至此,我们简易的markdown编译器已编写完毕,当然,现在的它并没有包含所有的markdown语法,待后期补充

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值