q.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. graceUI rich-text 鍔犲己宸ュ叿
  3. link : graceui.hcoder.net
  4. author : 5213606@qq.com 娣辨捣
  5. */
  6. // 姝e垯鍙橀噺
  7. var graceRichTextReg;
  8. // 鎵归噺鏇挎崲鐨勬牱寮� [ 鏍规嵁椤圭洰闇€姹傝嚜琛岃缃� ]
  9. var GRT = [
  10. // div 鏍峰紡
  11. ['div', "line-height:2em;"],
  12. // h1 鏍峰紡
  13. ['h1', "font-size:3em; line-height:1.5em;"],
  14. // h2 鏍峰紡
  15. ['h2', "font-size:2em; line-height:1.8em;"],
  16. // h3 鏍峰紡
  17. ['h3', "font-size:1.6em; line-height:2em;"],
  18. // h4 鏍峰紡
  19. ['h4', "font-size:1.2em; line-height:2em;"],
  20. // h5 鏍峰紡
  21. ['h5', "font-size:1em; line-height:2em;"],
  22. // h6 鏍峰紡
  23. ['h6', "font-size:0.9em; line-height:2em;"],
  24. // p 鏍峰紡
  25. ['p', "font-size:1em; line-height:2em;"],
  26. // b 鏍峰紡
  27. ['b', "font-size:1em; line-height:2em;"],
  28. // strong 鏍峰紡
  29. ['strong', "font-size:1em; line-height:2em;"],
  30. // code 鏍峰紡
  31. ['code', "font-size:1em; line-height:1.2em; background:#F6F7F8; padding:8px 2%; width:96%;"],
  32. // img 鏍峰紡
  33. ['img', "width:100%; margin:8px 0;"],
  34. // blockquote
  35. ['blockquote', "font-size:1em; border-left:3px solid #D1D1D1; line-height:2em; border-radius:5px; background:#F6F7F8; padding:8px 2%;"],
  36. // li 鏍峰紡
  37. ['ul', "padding:5px 0; list-style:none; padding:0; margin:0;"],
  38. ['li', "line-height:1.5em; padding:5px 0; list-style:none; padding:0; margin:0; margin-top:10px;"],
  39. // table
  40. ['table', "width:100%; border-left:1px solid #F2F3F4; border-top:1px solid #F2F3F4;"],
  41. ['th', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4;"],
  42. ['td', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4; padding-left:5px;"]
  43. ];
  44. module.exports = {
  45. format : function(html){
  46. html = html.replace(/<pre.*pre>?/gis, function(word){
  47. word = word.replace(/[\n]/gi,'<br />');
  48. word = word.replace(/ /gi,'<span style="padding-left:2em;"></span>');
  49. return word.replace(/[\t]/gi, '<span style="padding-left:2em;"></span>');
  50. });
  51. html = html.replace(/<pre/gi, '<p style="font-size:1em; margin:12px 0; line-height:1.2em; background:#F6F7F8; border-radius:5px; padding:8px 4%; width:92%;"');
  52. html = html.replace(/<\/pre/gi,"</p");
  53. for(let i = 0; i < GRT.length; i++){
  54. graceRichTextReg = new RegExp('<'+GRT[i][0]+'>|<'+GRT[i][0]+' (.*?)>', 'gi');
  55. html = html.replace(graceRichTextReg , function(word){
  56. // 鍒嗘瀽 dom 涓婃槸鍚﹀甫鏈� style=""
  57. if(word.indexOf('style=') != -1){
  58. var regIn = new RegExp('<' + GRT[i][0] + '(.*?)style="(.*?)"(.*?)(/?)>', 'gi');
  59. return word.replace(regIn, '<'+ GRT[i][0] +'$1style="$2 ' + GRT[i][1] +'"$3$4>');
  60. }else{
  61. var regIn = new RegExp('<' + GRT[i][0] + '(.*?)(/?)>', 'gi');
  62. return word.replace(regIn, '<'+ GRT[i][0] +'$1 style="' + GRT[i][1] +'$2">');
  63. }
  64. });
  65. }
  66. return html;
  67. }
  68. }