转于 第十四届蓝桥杯Web大学组第九题Markdown 文档解析,简单实现_蓝桥杯markdown文档解析-CSDN博客
修改了部分内容
// 是否为符合分隔符规范
isHr() {
return this.hr.test(this.lineText);
}
// 解析分隔符
parseHr() {
return `<hr>`;
}
// 是否为符合引用区块规范
isBlockQuote() {
return this.blockQuote.test(this.lineText);
}
// 解析引用区块
parseBlockQuoteStart() {
return `<blockquote>`;
}
parseBlockQuoteEnd() {
return `</blockquote>`;
}
parseBlockQuoteContent() {
const content = this.lineText.split("> ")[1];
return `<p>${content}</p>`;
}
// 是否为符合无序列表规范
isUnorderedList() {
return this.unorderedList.test(this.lineText);
}
// 解析无序列表
parseUnorderedListStart() {
return `<ul>`;
}
parseUnorderedListEnd() {
return `</ul>`;
}
parseUnorderedListContent() {
// 不知道为什么不可以
// const content = this.lineText.split("*")[1].trim() || this.lineText.split("-")[1].trim();
const content = this.lineText.split(" ")[1].trim();
return `<li>${content}</li>`;
}
// 是否为符合图片,和文字效果规范
isImage() {
return this.image.test(this.lineText);
}
isStrongTextCodeLine() {
return (
this.codeLine.test(this.lineText) || this.strongText.test(this.lineText)
);
}
// 解析图片,和文字效果
parseImage() {
let alt = this.lineText.split("![")[1].split("]")[0];
let src = this.lineText.split("(")[1].split(")")[0];
console.log(alt, src, "liwei");
return `<img src="${src}" alt="${alt}">`;
}
parseStrongTextCodeLine() {
return this.lineText
.replace("**", "<b>")
.replace("**", "</b>")
.replace("`", "<code>")
.replace("`", "</code>");
}
// 判断分隔符
if (this.parser.isHr()) {
hasParsed.push(this.parser.parseHr());
currentLine++;
continue;
}
// 判断引用区块
if (this.parser.isBlockQuote()) {
// 添加开头
hasParsed.push(this.parser.parseBlockQuoteStart());
while (true) {
if (this.reachToEndLine(currentLine)) {
// 当前不存在
// 添加结束标签
hasParsed.push(this.parser.parseBlockQuoteEnd());
break;
}
// 添加p标签和内容
hasParsed.push(this.parser.parseBlockQuoteContent());
currentLine++;
// 获取行文本
this.parser.parseLineText(this.getLineText(currentLine));
if (!this.parser.isBlockQuote()) {
// 当前不符合
// 添加结束标签
hasParsed.push(this.parser.parseBlockQuoteEnd());
break;
}
}
continue; //这样就会重新去判断
}
// 判断无序列表
if (this.parser.isUnorderedList()) {
hasParsed.push(this.parser.parseUnorderedListStart());
while (true) {
if (this.reachToEndLine(currentLine)) {
// 当前不存在
// 添加结尾
hasParsed.push(this.parser.parseUnorderedListEnd());
break;
}
// 添加内容 必须在end判断之前
hasParsed.push(this.parser.parseUnorderedListContent());
// 下一行
currentLine++;
// 获取下一行
this.parser.parseLineText(this.getLineText(currentLine));
if (!this.parser.isUnorderedList()) {
//这个在最后
// 当前不符合
// 添加结尾
hasParsed.push(this.parser.parseUnorderedListEnd());
break;
}
}
continue;
}
// 判断图片
if (this.parser.isImage()) {
hasParsed.push(this.parser.parseImage());
currentLine++;
continue;
}
// 判断文字效果
if(this.parser.isStrongTextCodeLine()){
hasParsed.push(this.parser.parseStrongTextCodeLine())
}