mdToVue.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const fs = require('fs');
  2. var path = require('path');
  3. let marked = require('marked');
  4. let package = require("../package.json");
  5. if (!marked) {
  6. console.log('you need npm i marked -D!');
  7. }
  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. this.$nextTick(()=>{
  58. let copy = this.copy;
  59. new copy('.copy',{
  60. target:res => {
  61. return res.previousElementSibling
  62. }
  63. });
  64. let demourl = 'https://nutui.jd.com/demo.html#'+this.$route.path;
  65. this.demourl = demourl;
  66. this.qrcode.toDataURL(demourl,{width:170},(err,url)=>{
  67. this.codeurl = url
  68. });
  69. });
  70. }
  71. }
  72. </script>`;
  73. /**
  74. *
  75. * @param {text} sorce 替换 头部信息
  76. */
  77. function insert(sorce) {
  78. var insert = sorce.indexOf('</h1>');
  79. if (insert > -1) {
  80. return sorce.substring(0, insert) + '<i class="qrcode"><a :href="demourl"><span>请使用手机扫码体验</span><img :src="codeurl" alt=""></a></i>' + sorce.substring(insert, sorce.length);
  81. } else {
  82. return sorce
  83. }
  84. }
  85. ///创建一个空文件
  86. /**
  87. *
  88. * @param {string} output 输出路径
  89. * @param {string} sorce 文件源
  90. * @param {boole} ishasCode 是否需要二维码
  91. */
  92. function createdFile(output, sorce, ishasCode) {
  93. var pathSrc = output;
  94. if (!ishasCode) {
  95. var res = insert(sorce);
  96. } else {
  97. var res = sorce;
  98. }
  99. fs.open(pathSrc, "w+", (err, fd) => {
  100. var bufs = `<template><div @click="dsCode">
  101. <div v-if="content" class="layer">
  102. <pre><span class="close-box" @click="closelayer"></span><div v-html="content"></div></pre>
  103. </div>`+ res + '</div></template>' + jsroot;
  104. var buf = new Buffer(bufs);
  105. if (typeof fd == 'number') {
  106. fs.writeSync(fd, buf, 0, buf.length, 0);
  107. } else {
  108. console.log(pathSrc, ' typeof fd != number 请改正文件')
  109. }
  110. })
  111. }
  112. /**
  113. *
  114. * @param {string} filePath 监听路径
  115. * @param {*} outPath 输出路径
  116. * @param {*} nohead 是否有头文件
  117. */
  118. function fileDisplay(filePath, outPath, nohead) {
  119. var rendererMd = new marked.Renderer();
  120. rendererMd.code = function (code, infostring, escaped) {
  121. var lang = (infostring || '').match(/\S*/)[0];
  122. if (this.options.highlight) {
  123. var out = this.options.highlight(code, lang);
  124. if (out != null && out !== code) {
  125. escaped = true;
  126. code = out;
  127. }
  128. }
  129. if (!lang) {
  130. return '<pre><code>'
  131. + (escaped ? code : escape(code, true))
  132. + '</code></pre>';
  133. }
  134. if (lang === 'html') {
  135. code = code.replace(/@latest/g, '@' + package.version)
  136. }
  137. return '<pre class="prettyprint"><span class="lang">' + lang + '</span><code class="'
  138. + this.options.langPrefix
  139. + escape(lang, true)
  140. + '">'
  141. + (escaped ? code : escape(code, true))
  142. + '</code><i class="copy" copy="copy" data-clipboard-action="copy" data-clipboard-target="code" title="复制代码"></i><i toast="toast" title="全屏"></i></pre>\n';
  143. };
  144. marked.setOptions({
  145. renderer: rendererMd,
  146. highlight: function (code) {
  147. return require('highlight.js').highlightAuto(code).value;
  148. },
  149. tables: true
  150. }, res => {
  151. })
  152. fs.readdir(filePath, (res, files) => {
  153. if (!res) {
  154. files.forEach(filename => {
  155. //获取当前文件绝对路径
  156. let filedir = path.join(filePath, filename);
  157. //文件写入
  158. fs.stat(filedir, (err, stats) => {
  159. if (!err) {
  160. if (stats.isDirectory()) {
  161. fileDisplay(filedir, outPath, nohead)
  162. } else {
  163. //判断文件类型是否是md文件
  164. if (/.md$/.test(filedir)) {
  165. //文件读取
  166. fs.readFile(filedir, 'utf-8', (err, data) => {
  167. let html = marked(data);
  168. let filedirarry = filedir.split('.');
  169. let fdurl = filedirarry[0];
  170. let fileNames = [];
  171. for (let i = fdurl.length - 1; i > -1; i--) {
  172. if (/[a-zA-Z]/.test(fdurl[i])) {
  173. fileNames.push(fdurl[i])
  174. } else {
  175. fileNames.push('-')
  176. }
  177. }
  178. let newName = fileNames.reverse().join('').split('-');
  179. let resName = '';
  180. if (newName.indexOf('doc') > -1) {
  181. resName = newName[newName.length - 2]
  182. } else {
  183. resName = newName[newName.length - 1]
  184. }
  185. createdFile(outPath + '/' + resName + '.vue', html, nohead)
  186. });
  187. //文件监听
  188. let fsWatcher = fs.watchFile(filedir, {
  189. persistent: true,
  190. persistent: 1000
  191. }, (err, data) => {
  192. // console.log(err,data,filedir);
  193. fs.readFile(filedir, 'utf-8', (err, data) => {
  194. let html = marked(data);
  195. let filedirarry = filedir.split('/');
  196. let fileNames = filedirarry[filedirarry.length - 2];
  197. createdFile(outPath + '/' + fileNames + '.vue', html, nohead)
  198. });
  199. });
  200. }
  201. }
  202. }
  203. })
  204. })
  205. }
  206. })
  207. }
  208. //md转 其他格式类型
  209. function MdToHtml(commomOption) {
  210. // commomOption = options;
  211. //获取所有的md 转html的结果
  212. fileDisplay(commomOption.entry, commomOption.output, commomOption.nohead);
  213. }
  214. MdToHtml.prototype.apply = function (compiler) {
  215. // console.log(compiler,'lls')
  216. };
  217. module.exports = MdToHtml;