main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const Audio = require('./lib/Audio'),
  2. tagsAndAttrs = require('./lib/tagsAndAttrs');
  3. class towxml {
  4. constructor(option) {
  5. const _ts = this;
  6. option = option || {};
  7. for (let i in option) {
  8. _ts.config[i] = option[i];
  9. };
  10. _ts.m = {};
  11. let mdOption = {
  12. html: true,
  13. xhtmlOut: true,
  14. typographer: true,
  15. breaks: true,
  16. highlight: function (code, lang, callback) {
  17. return _ts.m.highlight.highlightAuto(code).value;
  18. }
  19. };
  20. _ts.m.toJson = require('./lib/toJson');
  21. _ts.m.highlight = require('./plugins/hljs/index');
  22. _ts.m.md = require('./lib/markdown-it')(mdOption);
  23. _ts.m.md_sub = require('./plugins/markdown-it-sub');
  24. _ts.m.md_sup = require('./plugins/markdown-it-sup');
  25. _ts.m.md_ins = require('./plugins/markdown-it-ins');
  26. _ts.m.md_mark = require('./plugins/markdown-it-mark');
  27. _ts.m.md_emoji = require('./plugins/markdown-it-emoji');
  28. _ts.m.md_todo = require('./plugins/markdown-it-todoList');
  29. _ts.m.md.use(_ts.m.md_sub);
  30. _ts.m.md.use(_ts.m.md_sup);
  31. _ts.m.md.use(_ts.m.md_ins);
  32. _ts.m.md.use(_ts.m.md_mark);
  33. _ts.m.md.use(_ts.m.md_emoji);
  34. _ts.m.md.use(_ts.m.md_todo);
  35. _ts.m.md.renderer.rules.emoji = function (token, idx) {
  36. return '<g-emoji class="h2w__emoji h2w__emoji--' + token[idx].markup + '">' + token[idx].content + '</g-emoji>';
  37. };
  38. }
  39. /**
  40. * markdown转html
  41. */
  42. md2html(mdContent) {
  43. const _ts = this;
  44. return _ts.m.md.render(mdContent);
  45. }
  46. /**
  47. * html2json
  48. * @param {string} content html或markdown字符串
  49. * @param {string} type 'html'、'marddown'
  50. * @param {object} app 小程序对象
  51. */
  52. toJson(content, type, app){
  53. const _ts = this;
  54. type = type || 'html';
  55. let json = '',
  56. sortOutJson;
  57. if (type === 'markdown') {
  58. json = new _ts.m.toJson(_ts.md2html(content),app).getData();
  59. } else if (type === 'html') {
  60. json = new _ts.m.toJson(content,app).getData();
  61. };
  62. json.theme = 'light';
  63. if(app){
  64. // 定义播放器点击时的播放与暂停方法
  65. if(typeof app.__audioPlayAndPause__ !== 'function'){
  66. app.__audioPlayAndPause__ = (event)=>{
  67. let currentTarget = event.currentTarget || {},
  68. dataset = currentTarget.dataset || {},
  69. _el = dataset._el || {},
  70. id = _el._id || {},
  71. player = typeof app.data.__audioObj__ === 'object' ? app.data.__audioObj__[id] : undefined;
  72. // 正在播放中则暂停,否则就播放
  73. if(player && player.status !== 'play' && player.status !== 'update'){
  74. player.play();
  75. }else{
  76. player.pause();
  77. player.status = 'pause';
  78. };
  79. };
  80. };
  81. tagsAndAttrs.binds.forEach(item => {
  82. let aItem = item.split(':'),
  83. bindType = aItem[0], // 事件绑定类型
  84. evenType = aItem[1]; // 事件类型
  85. // 检查,如果有添加自定义事件,则运行该事件
  86. app[`__${bindType}_${evenType}`] = (event)=>{
  87. let funName = `event_${bindType}_${evenType}`,
  88. timer = `${funName}_timer`,
  89. runFun = app[funName];
  90. // 为audio标签绑定音频播放
  91. if(event &&
  92. event.type === 'tap' &&
  93. event.currentTarget &&
  94. event.currentTarget.dataset &&
  95. event.currentTarget.dataset._el &&
  96. event.currentTarget.dataset._el._e &&
  97. event.currentTarget.dataset._el._e.tagName === 'audio'){
  98. app.__audioPlayAndPause__(event);
  99. };
  100. if(typeof runFun === 'function'){
  101. // 由于小程序的事件绑定方式与冒泡机制问题,此处使用计时器以避免事件被同时多次调用
  102. clearTimeout(app[timer]);
  103. app[timer] = setTimeout(()=>{
  104. runFun(event)
  105. });
  106. };
  107. };
  108. });
  109. app[`__todo_checkboxChange`] = (event)=>{};
  110. };
  111. return json;
  112. }
  113. };
  114. module.exports = towxml;