md-to-other.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const fs = require('fs');
  2. var path = require('path');
  3. let marked = require('marked');
  4. if (!marked) {
  5. console.log('you need npm i marked -D!');
  6. }
  7. //let commomOption = null;
  8. //插入 默认的 script 标签
  9. let jsroot = `<script>export default {
  10. data(){
  11. return {
  12. content:'',
  13. codeurl:'',
  14. demourl:''
  15. }
  16. },
  17. methods:{
  18. closelayer(){
  19. this.content = '';
  20. },
  21. toast(e){
  22. const options = {
  23. noHeader:true,
  24. noFooter:true,
  25. content:e.target.parentElement.outerHTML
  26. }
  27. this.content = options.content;
  28. let copy = this.copy;
  29. new copy('.copy',{
  30. target:res => {
  31. return res.previousElementSibling
  32. }
  33. });
  34. },
  35. dsCode(e){
  36. let tag = e.target;
  37. if(tag.attributes.toast){
  38. this.toast(e)
  39. }
  40. }
  41. },
  42. mounted(){
  43. let that = this;
  44. let pre = document.querySelectorAll('pre');
  45. for(let i=0,item;item = pre[i];i++){
  46. item.classList.toggle('prettyprint');
  47. let creatC = document.createElement('i');
  48. creatC.setAttribute('copy','copy');
  49. creatC.setAttribute('data-clipboard-action','copy');
  50. creatC.setAttribute('data-clipboard-target','code');
  51. creatC.setAttribute('class','copy')
  52. let creatA = document.createElement('i');
  53. creatA.setAttribute('toast','toast');
  54. item.appendChild(creatC);
  55. item.appendChild(creatA);
  56. }
  57. let copy = this.copy;
  58. new copy('.copy',{
  59. target:res => {
  60. return res.previousElementSibling
  61. }
  62. });
  63. let demourl = 'http://nutui.jd.com/demo.html#'+that.$route.path;
  64. this.demourl = demourl;
  65. this.qrcode.toDataURL(demourl,{width:170},(err,url)=>{
  66. that.codeurl = url
  67. })
  68. }
  69. }
  70. </script>`;
  71. //插入
  72. function insert(sorce) {
  73. var insert = sorce.indexOf('</h1>');
  74. if (insert > -1) {
  75. return sorce.substring(0, insert) + '<i class="qrcode"><a :href="demourl"><span>请使用手机扫码体验</span><img :src="codeurl" alt=""></a></i>' + sorce.substring(insert, sorce.length);
  76. } else {
  77. return sorce
  78. }
  79. }
  80. ///创建一个空文件
  81. function createdFile(output, sorce,ishasCode) {
  82. var pathSrc = output;
  83. if(!ishasCode){
  84. var res = insert(sorce);
  85. } else{
  86. var res = sorce;
  87. }
  88. fs.open(pathSrc, "w+", (err, fd) => {
  89. var bufs = `<template><div @click="dsCode">
  90. <div v-if="content" class="layer">
  91. <span class="close-box" @click="closelayer"></span>
  92. <div v-html="content"></div>
  93. </div>`+ res + '</div></template>' + jsroot;
  94. var buf = new Buffer(bufs);
  95. fs.writeSync(fd, buf, 0, buf.length, 0);
  96. })
  97. }
  98. function fileDisplay(filePath,outPath,nohead) {
  99. var rendererMd = new marked.Renderer();
  100. marked.setOptions({
  101. renderer: rendererMd,
  102. highlight: function (code) {
  103. return require('highlight.js').highlightAuto(code).value;
  104. },
  105. tables: true
  106. }, res => {
  107. })
  108. fs.readdir(filePath, (res, files) => {
  109. if (!res) {
  110. files.forEach(filename => {
  111. //获取当前文件绝对路径
  112. let filedir = path.join(filePath, filename);
  113. //文件写入
  114. fs.stat(filedir, (err, stats) => {
  115. if (!err) {
  116. if (stats.isDirectory()) {
  117. fileDisplay(filedir,outPath,nohead)
  118. } else {
  119. //判断文件类型是否是md文件
  120. if (/.md$/.test(filedir)) {
  121. //文件读取
  122. fs.readFile(filedir, 'utf-8', (err, data) => {
  123. let html = marked(data);
  124. let filedirarry = filedir.split('.');
  125. let fdurl = filedirarry[0];
  126. let fileNames = [];
  127. for (let i = fdurl.length - 1; i > -1; i--) {
  128. if (/[a-zA-Z]/.test(fdurl[i])) {
  129. fileNames.push(fdurl[i])
  130. } else {
  131. fileNames.push('-')
  132. }
  133. }
  134. let newName = fileNames.reverse().join('').split('-');
  135. let resName = '';
  136. if(newName.indexOf('doc')>-1){
  137. resName = newName[newName.length - 2]
  138. }else{
  139. resName = newName[newName.length - 1]
  140. }
  141. createdFile(outPath + '/' + resName + '.vue', html,nohead)
  142. });
  143. //文件监听
  144. let fsWatcher = fs.watchFile(filedir, {
  145. persistent: true,
  146. persistent: 1000
  147. }, (err, data) => {
  148. // console.log(err,data,filedir);
  149. fs.readFile(filedir, 'utf-8', (err, data) => {
  150. let html = marked(data);
  151. let filedirarry = filedir.split('/');
  152. let fileNames = filedirarry[filedirarry.length - 2];
  153. createdFile(outPath + '/' + fileNames + '.vue', html,nohead)
  154. });
  155. });
  156. }
  157. }
  158. }
  159. })
  160. })
  161. }
  162. })
  163. }
  164. //md转 其他格式类型
  165. function MdToHtml(commomOption) {
  166. // commomOption = options;
  167. //获取所有的md 转html的结果
  168. fileDisplay(commomOption.entry,commomOption.output,commomOption.nohead);
  169. }
  170. MdToHtml.prototype.apply = function (compiler) {
  171. // console.log(compiler,'lls')
  172. };
  173. module.exports = MdToHtml;