index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * FeHelper HTML转Markdown
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. sourceContent: '',
  8. resultContent: '',
  9. previewHTML: '',
  10. showPreview: false,
  11. previewText: '效果预览',
  12. codeType: 'HTML',
  13. nextCodeType: 'Markdown',
  14. toolName: {
  15. HTML: 'HTML转Markdown',
  16. Markdown: 'Markdown转HTML'
  17. }
  18. },
  19. mounted: function () {
  20. this.$refs.srcText.focus();
  21. },
  22. methods: {
  23. convert: function () {
  24. let h2m = Tarp.require('../static/vendor/h2m/h2m');
  25. let markdown = Tarp.require('../static/vendor/h2m/markdown');
  26. if (this.codeType === 'HTML') {
  27. this.resultContent = h2m(this.sourceContent, {
  28. converter: 'CommonMark' // CommonMark | MarkdownExtra
  29. });
  30. this.previewHTML = markdown.toHTML(this.resultContent);
  31. } else {
  32. this.resultContent = this.previewHTML = markdown.toHTML(this.sourceContent);
  33. }
  34. },
  35. preview: function (event) {
  36. event && event.preventDefault();
  37. this.showPreview = !this.showPreview;
  38. this.previewText = this.showPreview ? '查看源码' : '效果预览';
  39. },
  40. trans: function () {
  41. this.codeType = {HTML: 'Markdown', Markdown: 'HTML'}[this.codeType];
  42. this.nextCodeType = {HTML: 'Markdown', Markdown: 'HTML'}[this.nextCodeType];
  43. this.preview();
  44. this.clear();
  45. },
  46. clear: function () {
  47. this.sourceContent = '';
  48. this.resultContent = '';
  49. this.resultContent = false;
  50. },
  51. getResult: function () {
  52. this.$refs.rstCode.select();
  53. },
  54. setDemo: function () {
  55. if (this.codeType === 'HTML') {
  56. this.sourceContent = this.$refs.htmlDemo.innerHTML;
  57. } else {
  58. this.sourceContent = '## FE助手\n' +
  59. '\n' +
  60. '- 字符串编解码\n' +
  61. '- `Json`串格式化\n' +
  62. '- 代码美化工具\n' +
  63. '- 代码压缩工具\n' +
  64. '- 二维码生成器\n' +
  65. '- 页面取色工具\n' +
  66. '- Js正则表达式\n' +
  67. '- 时间(戳)转换\n' +
  68. '- 图片Base64\n' +
  69. '- 编码规范检测\n' +
  70. '- 页面性能检测\n' +
  71. '- Html转`Markdown`\n' +
  72. '- Ajax调试:**关**';
  73. }
  74. this.convert();
  75. },
  76. // 导入内容
  77. importContent: function () {
  78. let that = this;
  79. let fileInput = document.getElementById('fileInput');
  80. if (!fileInput) {
  81. fileInput = document.createElement('input');
  82. fileInput.id = 'fileInput';
  83. fileInput.type = 'file';
  84. fileInput.accept = {HTML: 'text/html', Markdown: 'text/x-markdown'}[that.codeType];
  85. fileInput.style.cssText = 'position:relative;top:-1000px;left:-1000px;';
  86. fileInput.onchange = function (event) {
  87. let reader = new FileReader();
  88. reader.readAsText(fileInput.files[0], 'utf-8');
  89. reader.onload = (evt) => {
  90. that.sourceContent = evt.target.result;
  91. that.convert();
  92. document.body.removeChild(fileInput);
  93. };
  94. };
  95. document.body.appendChild(fileInput);
  96. }
  97. fileInput.click();
  98. },
  99. // 通过调用系统打印的形式,打印为pdf
  100. exportContent: function () {
  101. let css = `@page {
  102. size: 5.5in 8.5in;
  103. margin: 70pt 60pt 70pt;
  104. }
  105. @page:first {
  106. size: 5.5in 8.5in;
  107. margin: 0;
  108. }
  109. img {
  110. max-width: 100%;
  111. }
  112. div.frontcover {
  113. page: cover;
  114. content: url("images/cover.png");
  115. width: 100%;
  116. height: 100%;
  117. }
  118. /* styles for the right hand spread
  119. Bottom left we display the title of the book, bottom right the page using a CSS counter, top right the content of the current chapter */
  120. @page:right{
  121. @bottom-left {
  122. margin: 10pt 0 30pt 0;
  123. border-top: .25pt solid #666;
  124. content: "Our Cats";
  125. font-size: 9pt;
  126. color: #333;
  127. }
  128. @bottom-right {
  129. margin: 10pt 0 30pt 0;
  130. border-top: .25pt solid #666;
  131. content: counter(page);
  132. font-size: 9pt;
  133. }
  134. @top-right {
  135. content: string(doctitle);
  136. margin: 30pt 0 10pt 0;
  137. font-size: 9pt;
  138. color: #333;
  139. }
  140. }
  141. /* styles for the left hand spread
  142. Bottom right book title, bottom left current page */
  143. @page:left {
  144. @bottom-right {
  145. margin: 10pt 0 30pt 0;
  146. border-top: .25pt solid #666;
  147. content: "Our Cats";
  148. font-size: 9pt;
  149. color: #333;
  150. }
  151. @bottom-left {
  152. margin: 10pt 0 30pt 0;
  153. border-top: .25pt solid #666;
  154. content: counter(page);
  155. font-size: 9pt;
  156. }
  157. }
  158. /* first page */
  159. @page:first {
  160. @bottom-right {
  161. content: normal;
  162. margin: 0;
  163. }
  164. @bottom-left {
  165. content: normal;
  166. margin: 0;
  167. }
  168. }
  169. /* reset chapter and figure counters on the body */
  170. body {
  171. counter-reset: chapternum figurenum;
  172. font-family: "Trebuchet MS", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Tahoma, sans-serif;
  173. line-height: 1.5;
  174. font-size: 11pt;
  175. }
  176. /* get the title of the current chapter - this will be the content of the h1
  177. reset figure counter as figures start from 1 in each chapter */
  178. h1 {
  179. string-set: doctitle content();
  180. page-break-before: always;
  181. counter-reset: figurenum;
  182. counter-reset: footnote;
  183. line-height: 1.3;
  184. }
  185. /* increment chapter counter */
  186. h1.chapter:before {
  187. counter-increment: chapternum;
  188. content: counter(chapternum) ". ";
  189. }
  190. /* increment and display figure counter */
  191. figcaption:before {
  192. counter-increment: figurenum;
  193. content: counter(chapternum) "-" counter(figurenum) ". ";
  194. }
  195. /* footnotes */
  196. .fn {
  197. float: footnote;
  198. }
  199. .fn {
  200. counter-increment: footnote;
  201. }
  202. .fn::footnote-call {
  203. content: counter(footnote);
  204. font-size: 9pt;
  205. vertical-align: super;
  206. line-height: none;
  207. }
  208. .fn::footnote-marker {
  209. font-weight: bold;
  210. }
  211. @page {
  212. @footnotes {
  213. border-top: 0.6pt solid black;
  214. padding-top: 8pt;
  215. }
  216. }
  217. h1,h2,h3,h4,h5 {
  218. font-weight: bold;
  219. page-break-after: avoid;
  220. page-break-inside:avoid;
  221. }
  222. h1+p, h2+p, h3+p {
  223. page-break-before: avoid;
  224. }
  225. table, figure {
  226. page-break-inside: avoid;
  227. }
  228. ul.toc {
  229. list-style: none;
  230. margin: 0;
  231. padding: 0;
  232. }
  233. /* create page numbers using target-counter in the TOC */
  234. ul.toc a::after {
  235. content: leader('.') target-counter(attr(href), page);
  236. }
  237. ul.toc li {
  238. line-height: 2;
  239. }
  240. ul.toc li a {
  241. text-decoration: none;
  242. }
  243. a {
  244. color: #000;
  245. }
  246. /* add page number to cross references */
  247. a.xref:after {
  248. content: " (page " target-counter(attr(href, url), page) ")";
  249. }
  250. `;
  251. let newContent = "<html><head><meta charset='utf-8'/><title></title>" +
  252. "<style>" + css + "</style>" +
  253. "</head><body>" +
  254. this.previewHTML + "</body></html>";
  255. let newWin = window.open();
  256. newWin.focus();
  257. newWin.document.write(newContent);
  258. newWin.print();
  259. newWin.document.close();
  260. newWin.close();
  261. }
  262. }
  263. })
  264. ;