js实现markdown转html

《js实现markdown转html》

页面如下:

在这里插入图片描述

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>mdToHtml</title>
    <link rel="stylesheet" href="mdToHtml.css">
</head>
<body>
    <div class="content">
        <div id='con1' class="div">MarkDown</div>
        <div id="con2" class="div">>>>></p>
            <div>
                <input type="file" name = "file" id = "fileId"> 
                <button  type = "submit" name = "btn" value = "提交" id = "btnId" onclick="check()" > 提交</button>
            </div>
        </div>
        <div id="con3" class="div">Html</div>
    </div>
   <script src="mdToHtml.js"></script> 
</body>
</html>

CSS:

.content{
    width:900px;
    height: 300px;
    margin: 250px auto;
}
.div{
    float: left;
    height: 100%;
    width: 33%;
    font-size: 50px;
    text-align: center;
    border: 1px solid black; 
}
#con1{
    background-color: aquamarine;
}
#con2{
    background-color: rebeccapurple;
}
#con3{
    background-color:khaki;
}
#btnId{
    width: 200px;
    height: 60px;
    font-size: 30px;
    border: 2px solid blue;
    border-radius: 10px;
}

JS:

思路分析:
1.xx.md格式文件的上传;
2.文件的内容转化;
3.xx.html格式文件的下载;

1.xx.md格式文件的上传:

function check() {
  button = document.getElementById("btnId");
  objFile = document.getElementById("fileId");
  if (objFile.value == "") {
    alert("不能空");
  }
  var files = objFile.files; //获取到文件列表
  console.log(files);
  if (files.length == 0) {
    alert("请选择文件");
  } else {
    valuePos = objFile.value; //存下此次提交的文件名
    var reader = new FileReader(); //新建一个FileReader
    reader.readAsText(files[0], "UTF-8"); //读取文件
    reader.onload = function(evt) {
      //读取完文件之后会回来这里
      fileString = evt.target.result; // 读取文件内容(全局)
      //设置定时器时刻检测是否用户未下载又重新提交一次不一样的文件;
      setInterval(function() {
        if (objFile.value !== valuePos) {
          button.innerHTML = "提交";
          button.onclick = function() {
            check();
            valuePos = objFile.value; //存下此次提交的文件名,
          };
        }
      }, 30);
      change();
    };
  }
}

2.文件的内容转化:

function change() {
  var nameReg = /\w+\./gi;
  name = valuePos.match(nameReg);
  result = [
    "<!DOCTYPE html>",
    '<html lang="en">',
    "<head>",
    '    <meta charset="UTF-8">',
    '    <meta name="viewport" content="width=device-width, initial-scale=1.0">',
    '    <meta http-equiv="X-UA-Compatible" content="ie=edge">',
    "    <title>"
  ].join("");
  result += name + "html" + "</title>";
  ///css
  result += "<style>div{width: 50%;margin: auto;}img{max-width:100%}</style>";
  var chImg = /(!\[[\S| ]+\])(\()([\S| ]+)(\))/gi;
  fileString = fileString.replace(chImg, '<img src="$3">'); //图片
  var chA = /(##\s+\[)([\S| ]+)(\]\()(\S+)\)/gi;
  fileString = fileString.replace(/#(?=\S+\))/g,'%23');//先将链接中的#转化为%23;
  fileString = fileString.replace(chA, '<h2><a href="$4">$2</a></h2>');//标题超链接
  fileString = fileString.replace(/(\[)([\S| ]+)(\]\()(\S+)\)/g, "<a href='$4'>$2</a>");//非标题超链接
  fileString = fileString.replace(/[\n|\r]+/g, "</br>");//空格
  result += "<body><div>";//div居中宽50%;
  result += fileString;
  result += "</div></body>";
  //布局收尾
  button.innerHTML = "下载HTML";
  button.onclick = function() {
    download();
  };
}

3.xx.html格式文件的下载:

function download() {
  var a = document.createElement("a");
  a.download = name + "html";
  result += "</html>";
  a.href = "data:text/txt;charset=utf-8," + result;
  a.textContent = "download";
  a.click();
  button = document.getElementById("btnId");
  button.innerHTML = "提交";
  button.onclick = function() {
    check();
  };
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值