towxml.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const towxml = require('./index.js');
  2. Component({
  3. options:{
  4. styleIsolation:'shared'
  5. },
  6. properties:{
  7. nodes:{
  8. type:Object,
  9. value:{}
  10. },
  11. markdown:{
  12. type:String,
  13. value:''
  14. },
  15. theme:{
  16. type:String,
  17. value:'light'
  18. }
  19. },
  20. observers:{
  21. 'markdown': function(markdown){
  22. if(markdown && typeof markdown === 'string'){
  23. try{
  24. const nodes = towxml(markdown, 'markdown', {
  25. theme: this.data.theme || 'light',
  26. base: ''
  27. });
  28. this.setData({
  29. nodes: nodes
  30. });
  31. }catch(error){
  32. console.error('Markdown 转换失败:', error);
  33. }
  34. }
  35. }
  36. },
  37. data:{
  38. nodes:{}
  39. },
  40. lifetimes:{
  41. attached(){
  42. // 如果传入了 markdown 文本,进行转换
  43. if(this.properties.markdown){
  44. try{
  45. const nodes = towxml(this.properties.markdown, 'markdown', {
  46. theme: this.properties.theme || 'light',
  47. base: ''
  48. });
  49. this.setData({
  50. nodes: nodes
  51. });
  52. }catch(error){
  53. console.error('Markdown 转换失败:', error);
  54. }
  55. }
  56. }
  57. }
  58. })