第十四届蓝桥杯Web大学组第九题Markdown 文档解析()

转于 第十四届蓝桥杯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())
      }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值