简单的Markdown解析器

随笔3周前发布 郑月泰
29 0 0

什么是markdown?

markdown是一种可以使用普通文本编辑器编写的标记语言。它可以通过简单的标记语法使普通文本内容具有一定的格式。Markdown的语法简洁明了、学习容易,而且功能比纯文本更强,因此有很多人用它写博客。

marked解析markdown文本

简单的解析器,如图

简单的Markdown解析器

<!doctype html>
<html>

<head>
  <meta charset="utf-8" />
  <title>markdown</title>
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/marked/0.8.1/marked.min.js"></script>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .container {
      width: 100%;
      height: 100vh;
      display: flex;
    }

    .left,
    .right {
      width: 50vw;
      height: 100vh;
      overflow-y: scroll;
    }

    .line {
      border-left: 1px solid #cccccc;
    }

    .textarea {
      width: 100%;
      height: 100%;
      border: none;
      border: none;
      outline: none;
      resize: none;
      background: #f2f2f2;
      font-size: 16px;
      text-indent: 2px;
      padding: 20px 0;
    }

    blockquote {
      background: #f2f2f2;
      padding: 20px;
      margin: 20px;
    }

    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
      padding-left: 20px;
      padding-top: 20px;
    }

    a {
      padding: 20px 0;
    }

    pre {
      margin: 10px;
      padding: 20px;
    }

    ::-webkit-scrollbar {
      width: 0 !important
    }
  </style>
</head>

<body>

  <div class="container">
    <div class="left">
      <textarea class="textarea"></textarea>
    </div>
    <div class="line"></div>
    <div class="right">
    </div>
  </div>
  <script>
    $(function () {
      $(`body`).delegate('textarea', 'propetychange input', function () {
        $('.right').html(marked.parse($(this).val()));
        //hljs.highlightAll();
      })
      $('.textarea').scroll(function () {
        $('.right').scrollTop($(this).scrollTop());
        $('.right').scrollLeft($(this).scrollLeft());
      });
      $('.right').scroll(function () {
        $('.textarea').scrollTop($(this).scrollTop());
        $('.textarea').scrollLeft($(this).scrollLeft());
      });
    })
  </script>
</body>

</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

扩展成高亮展示

引用
<link rel="stylesheet" href="http://yandex.st/highlightjs/8.0/styles/solarized_dark.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>

  • 1
  • 2

调用 hljs.highlightAll();

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...