build.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const config = require('../config')
  2. const build = {
  3. import: 'prism.css',
  4. handler (file) {
  5. if (file.path.includes('prism.css')) {
  6. // 将标签名选择器和属性选择器转为 class 选择器(组件内仅支持 class 选择器)
  7. file.contents = Buffer.from(file.contents.toString().replace(/pre([[)])/g, '.hl-pre$1').replace(/code/g, '.hl-code').replace(/\[class\*="?language-"?\]/g, '').replace(/:not[^,}]+[,}]*/g, '').replace(/\.token\./g, '.hl-'))
  8. }
  9. }
  10. }
  11. if (config.showLanguageName || config.showLineNumber) {
  12. // pre 内部的 code 进行滚动,避免行号和语言名称跟随滚动
  13. build.style = `.hl-pre {
  14. position: relative;
  15. }
  16. .hl-code {
  17. overflow: auto;
  18. display: block;
  19. }`
  20. }
  21. if (config.copyByLongPress) {
  22. build.template = '<rich-text v-if="n.attrs&&n.attrs[\'data-content\']" :nodes="[n]" :data-content="n.attrs[\'data-content\']" :data-lang="n.attrs[\'data-lang\']" @longpress="copyCode" />'
  23. build.methods = {
  24. copyCode (e) {
  25. uni.showActionSheet({
  26. itemList: ['复制代码'],
  27. success: () =>
  28. uni.setClipboardData({
  29. data: e.currentTarget.dataset.content
  30. })
  31. })
  32. }
  33. }
  34. }
  35. if (config.showLanguageName) {
  36. build.style = (build.style || '') +
  37. `.hl-language {
  38. font-size: 12px;
  39. font-weight: 600;
  40. position: absolute;
  41. right: 8px;
  42. text-align: right;
  43. top: 3px;
  44. }
  45. .hl-pre {
  46. padding-top: 1.5em;
  47. }`
  48. }
  49. if (config.showLineNumber) {
  50. build.style = (build.style || '') +
  51. `.hl-pre {
  52. font-size: 14px;
  53. padding-left: 3.8em;
  54. counter-reset: linenumber;
  55. }
  56. .line-numbers-rows {
  57. position: absolute;
  58. pointer-events: none;
  59. top: ${config.showLanguageName ? 1.5 : 1}em;
  60. font-size: 100%;
  61. left: 0;
  62. width: 3em; /* works for line-numbers below 1000 lines */
  63. letter-spacing: -1px;
  64. border-right: 1px solid #999;
  65. -webkit-user-select: none;
  66. -moz-user-select: none;
  67. -ms-user-select: none;
  68. user-select: none;
  69. }
  70. .line-numbers-rows .span {
  71. display: block;
  72. counter-increment: linenumber;
  73. }
  74. .line-numbers-rows .span:before {
  75. content: counter(linenumber);
  76. color: #999;
  77. display: block;
  78. padding-right: 0.8em;
  79. text-align: right;
  80. }`
  81. }
  82. module.exports = build