mdToVue.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. const path = require('path');
  2. const fs = require('fs');
  3. const nodeFilelist = require('node-filelist');
  4. let { hashElement } = require('folder-hash');
  5. let { version } = require("../package.json");
  6. let marked = require('marked');
  7. let contrast = require('./contrast');
  8. let rimraf = require("rimraf");
  9. if (!marked) {
  10. console.log('you need npm i marked -D!');
  11. }
  12. class mdVue{
  13. constructor(options){
  14. let defaultSetting = {
  15. needCode:true,
  16. isbuild:true,
  17. hasMarkList:true
  18. };
  19. this.options =Object.assign(defaultSetting,options);
  20. this.rendererMd = new marked.Renderer();
  21. this.marked = marked;
  22. let _that = this;
  23. this.Articlehead = "";
  24. this.Articleheadcount = 0;
  25. this.needHandleFiles = null;
  26. this.cachePath = path.join("./cache",options.entry.split(path.sep).pop()+ '.cache');
  27. this.codeHandle();
  28. this.checkSelf().then(res=>{
  29. // 检查要编译的文件
  30. let ct = new contrast({
  31. entry:this.options.entry
  32. }).run().then(res=>{
  33. _that.needHandleFiles = res;
  34. _that.ishasOutFile(_that.options.output).then(res=>{
  35. _that.star();
  36. });
  37. })
  38. })
  39. }
  40. checkSelf(){
  41. let _that = this;
  42. return new Promise((resolve,reject)=>{
  43. new contrast({
  44. entry:path.join(__dirname,"mdToVue.js"),
  45. include:["*.js"]
  46. }).run().then(res=>{
  47. if(JSON.stringify(res) != "{}"){
  48. //有变动清除当前entry缓存,重新渲染
  49. rimraf(_that.cachePath,err=>{
  50. console.log(err)
  51. resolve()
  52. })
  53. }else{
  54. resolve()
  55. }
  56. })
  57. })
  58. }
  59. apply(){}
  60. star(){
  61. let _that = this;
  62. hashElement(_that.options.entry, {
  63. folders: { exclude: ['.*', 'node_modules', 'test_coverage'] },
  64. files: { include: ['*.md']},
  65. matchBasename: true
  66. }).then(hash => {
  67. nodeFilelist.read([_that.options.entry],{"ext":'md'}, res => {
  68. res.map(item =>{
  69. let fileSplits = item.path.split(path.sep);
  70. let fileName = fileSplits.pop();
  71. if(_that.isDoc(fileName)){
  72. fileName = fileSplits.pop();
  73. }else{
  74. fileName = fileName.replace(/\.md/,'');
  75. }
  76. if(_that.needHandleFiles[fileName]){
  77. _that.read(item.path).then(res=>{
  78. _that.headHandle();
  79. _that.markHandle();
  80. let html = _that.marked(res);
  81. _that.write({
  82. outsrc:_that.options.output,
  83. name:fileName + '.vue',
  84. html:html
  85. });
  86. })
  87. }
  88. });
  89. });
  90. })
  91. }
  92. isDoc(name){
  93. return (name == "doc.md") ? true : false;
  94. }
  95. read(src){
  96. return new Promise((resolve,reject)=>{
  97. fs.readFile(src, 'utf-8', (err, data) => {
  98. resolve(data)
  99. })
  100. })
  101. }
  102. write(param){
  103. let _that = this;
  104. return new Promise((resolve,reject)=>{
  105. let outPath = path.join(param.outsrc,param.name);
  106. let contexts = `<template>
  107. <div @click="dsCode">
  108. <div v-if="content" class="layer">
  109. <pre><span class="close-box" @click="closelayer"></span><div v-html="content"></div></pre>
  110. </div>`+ param.html + (_that.options.hasMarkList ? '<ul class="markList">' + _that.Articlehead + '</ul>' : '')+
  111. `<nut-backtop :right="50" :bottom="50"></nut-backtop>
  112. </div>
  113. </template><script>import root from '../root.js';
  114. export default {
  115. mixins:[root]
  116. }</script>`;
  117. _that.Articlehead = "";
  118. _that.Articleheadcount = 0;
  119. fs.writeFile(outPath ,contexts,'utf8',(err,res)=>{ })
  120. })
  121. }
  122. ishasOutFile(outPath){
  123. return new Promise((resolve,reject)=>{
  124. fs.stat(outPath,(err,res)=>{
  125. if(err){
  126. fs.mkdir(outPath,err=>{
  127. if(err){
  128. reject(err)
  129. }else{
  130. resolve(true)
  131. }
  132. })
  133. }else{
  134. resolve(true)
  135. }
  136. })
  137. })
  138. }
  139. headHandle(){
  140. let _that = this;
  141. let options = _that.options;
  142. _that.rendererMd.heading = function (text,level){
  143. let headcode = '<i class="qrcode"><a :href="demourl"><span>请使用手机扫码体验</span><img :src="codeurl" alt=""></a></i>';
  144. let codeHead = `<h1>` + text +headcode+`</h1>`;
  145. let normal = `<h`+level+`>`+text+`</h`+level+`>`;
  146. let Articleheadcounts = null;
  147. if(level==2&&_that.options.hasMarkList){
  148. Articleheadcounts = _that.Articleheadcount;
  149. Articleheadcounts++;
  150. _that.Articleheadcount = Articleheadcounts;
  151. let headmsg = _that.Articlehead;
  152. headmsg +=`<li @click="leavelchose(`+Articleheadcounts+`)" :class="levalcur==`+Articleheadcounts+`?'cur':''" class="level`+Articleheadcounts+'"><a href="#head'+Articleheadcounts+'">'+text.substr(0,10)+'</a></li>';
  153. _that.Articlehead = headmsg;
  154. }
  155. let maskIdHead = "<h"+level+" class='visibility' id='head"+Articleheadcounts+"'>"+ text +"</h"+level+">";
  156. //判断条件
  157. if(_that.options.hasMarkList && _that.options.needCode){
  158. if(level == 1){
  159. return codeHead;
  160. }else if(level == 2){
  161. return maskIdHead;
  162. } else{
  163. return normal;
  164. }
  165. }else if(_that.options.hasMarkList && !_that.options.needCode){
  166. if(level !=1){
  167. return maskIdHead;
  168. } else{
  169. return normal;
  170. }
  171. }else if(!_that.options.hasMarkList && _that.options.needCode){
  172. if(level == 1){
  173. return codeHead;
  174. } else{
  175. return normal;
  176. }
  177. }else if(!_that.options.hasMarkList && !_that.options.needCode){
  178. return normal;
  179. }
  180. };
  181. }
  182. codeHandle(){
  183. this.rendererMd.code = function (code, infostring, escaped) {
  184. var lang = (infostring || '').match(/\S*/)[0];
  185. if (this.options.highlight) {
  186. var out = this.options.highlight(code, lang);
  187. if (out != null && out !== code) {
  188. escaped = true;
  189. code = out;
  190. }
  191. }
  192. if (!lang) {
  193. return '<pre><code>'
  194. + (escaped ? code : escape(code, true))
  195. + '</code></pre>';
  196. }
  197. if (lang === 'html') {
  198. code = code.replace(/@latest/g, '@' + version)
  199. }
  200. return '<hide><pre class="prettyprint"><span class="lang">' + lang + '</span><div class="code-wrapper"><code class="'
  201. + this.options.langPrefix
  202. + escape(lang, true)
  203. + '">'
  204. + (escaped ? code : escape(code, true))
  205. + '</code></div><i class="copy" copy="copy" data-clipboard-action="copy" data-clipboard-target="code" title="复制代码"></i><i toast="toast" title="全屏"></i></pre></hide>\n';
  206. };
  207. }
  208. markHandle(){
  209. let _that = this;
  210. this.marked.setOptions({
  211. renderer: _that.rendererMd,
  212. highlight: function (code) {
  213. return require('highlight.js').highlightAuto(code).value;
  214. },
  215. tables: true
  216. });
  217. }
  218. };
  219. module.exports = mdVue;