parsers.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. function parseMd(content) {
  2. var blockArray = [{title: ''}];
  3. var blockCount;
  4. var preDataIndex, imgDataIndex;
  5. var parseBlockDiv;
  6. var preImg, preImgObject;
  7. var preImgObject = {'image': {}}
  8. var isNotFrontMatterCount = 0;
  9. var isAmpImgRange = false;
  10. for (const line of content.split('\n')) {
  11. if (isNotFrontMatterCount < 2) {
  12. if (line.includes('---')) {
  13. isNotFrontMatterCount += 1;
  14. }
  15. continue;
  16. }
  17. if (isAmpImgRange === true && !(line.includes('</amp-img>'))) {
  18. imgParamObject = parseAmpImg(line);
  19. preImgObject.image = {...preImgObject.image, ...imgParamObject};
  20. const key = _.get(_.keys(imgParamObject), 0);
  21. preImg.setAttribute(key, _.get(imgParamObject, `${key}`));
  22. continue;
  23. }
  24. if (line.includes('##')) {
  25. var preDataIndex = 0;
  26. blockCount = blockCount + 1 | 0;
  27. const preBlockindex = blockCount;
  28. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements(contentDiv);
  29. parseBlockDiv = blockDiv;
  30. const title = parseTitle(line);
  31. const text = title;
  32. blockArray[preBlockindex] = {title: text};
  33. h.textContent = text;
  34. inputButton.onclick = function() {
  35. const text = titleInput.value;
  36. blockArray[preBlockindex].title = text;
  37. h.textContent = text;
  38. }
  39. } else if (line.includes('amp-img')) {
  40. const preBlockindex = blockCount;
  41. if (line.includes('</amp-img>')) {
  42. blockArray = addDataToBlockArray(preImgObject, blockArray, preBlockindex, imgDataIndex)
  43. isAmpImgRange = false;
  44. continue;
  45. }
  46. imgDataIndex = preDataIndex;
  47. preDataIndex += 1;
  48. isAmpImgRange = true;
  49. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(parseBlockDiv);
  50. preImg = img;
  51. imgInputButton.onclick = function() {
  52. preImg.setAttribute('src', imgInput.value);
  53. preImg.setAttribute('width', widthInput.value);
  54. preImg.setAttribute('height', heightInput.value);
  55. preImg.setAttribute('alt', 'image field');
  56. const imgObject = {image: {src: imgInput.value,
  57. height: heightInput.value,
  58. width: widthInput.value,
  59. alt: 'image field',
  60. layout: 'responsive'}};
  61. blockArray = addDataToBlockArray(imgObject, blockArray, preBlockindex, imgDataIndex)
  62. }
  63. } else {
  64. const preBlockindex = blockCount;
  65. const ownDataIndex = preDataIndex;
  66. preDataIndex += 1;
  67. const {p, descTextArea, descInputButton} = getdescElements(parseBlockDiv);
  68. const text = line;
  69. p.textContent = text;
  70. blockArray = addDataToBlockArray({description: {text: text}}, blockArray, preBlockindex, ownDataIndex)
  71. descInputButton.onclick = function () {
  72. const text = descTextArea.value;
  73. p.textContent = text;
  74. addDataToBlockArray({description: {text: text}}, blockArray, preBlockindex, ownDataIndex)
  75. }
  76. }
  77. }
  78. return {blockArray: blockArray, blockCount: blockCount}
  79. }
  80. const parseTitle = line => {
  81. var title = '';
  82. title = line.replace('## **', '');
  83. title = title.replace('**', '');
  84. return title;
  85. };
  86. const parseAmpImg = line => {
  87. if (line.includes('alt')) {
  88. const altParameter = line.replace(/alt=|"/g, '');
  89. return {alt: altParameter};
  90. } else if (line.includes('src')) {
  91. const srcParameter = line.replace(/src=|"/g, '');
  92. return {src: srcParameter};
  93. } else if (line.includes('height')) {
  94. const heightParameter = line.replace(/height=|"/g, '');
  95. return {height: heightParameter};
  96. } else if (line.includes('width')) {
  97. const widthParameter = line.replace(/width=|"/g, '');
  98. return {width: widthParameter};
  99. } else if (line.includes('layout')) {
  100. const layoutParameter = line.replace(/layout=|"|>/g, '');
  101. }
  102. }