editor.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. function getBlockElements() {
  2. const blockDiv = document.createElement("div");
  3. const descButton = document.createElement('button');
  4. descButton.textContent = 'Add Description';
  5. const imgButton = document.createElement('button');
  6. imgButton.textContent = 'Add Image';
  7. const h = document.createElement("H2");
  8. const input = document.createElement("INPUT");
  9. input.setAttribute("type", "text");
  10. const inputButton = document.createElement('button')
  11. inputButton.textContent = 'Title新增/修改';
  12. contentDiv.append(blockDiv);
  13. blockDiv.appendChild(h);
  14. blockDiv.appendChild(input);
  15. blockDiv.appendChild(inputButton);
  16. blockDiv.appendChild(descButton);
  17. blockDiv.appendChild(imgButton);
  18. return {
  19. blockDiv: blockDiv,
  20. h: h,
  21. titleInput: input,
  22. inputButton: inputButton,
  23. descButton: descButton,
  24. imgButton: imgButton
  25. };
  26. }
  27. function getdescElements(blockDiv){
  28. const descDiv = document.createElement("div");
  29. const p = document.createElement("p");
  30. const descTextArea = document.createElement("TEXTAREA");
  31. descTextArea.setAttribute("type", "text");
  32. const descInputButton = document.createElement('button')
  33. descInputButton.textContent = 'Description新增/修改';
  34. descDiv.appendChild(p);
  35. descDiv.appendChild(descTextArea);
  36. descDiv.appendChild(descInputButton);
  37. blockDiv.appendChild(descDiv);
  38. return {
  39. p: p,
  40. descTextArea: descTextArea,
  41. descInputButton: descInputButton
  42. };
  43. }
  44. function getImgElements(blockDiv) {
  45. const imgDiv = document.createElement("div");
  46. const img = document.createElement("img");
  47. const imgInput = document.createElement("INPUT");
  48. const widthInput = document.createElement("INPUT");
  49. const heightInput = document.createElement("INPUT");
  50. const imgInputButton = document.createElement('button');
  51. imgInputButton.textContent = 'Image新增/修改';
  52. imgDiv.appendChild(img);
  53. imgDiv.appendChild(imgInput);
  54. imgDiv.appendChild(widthInput);
  55. imgDiv.appendChild(heightInput);
  56. imgDiv.appendChild(imgInputButton);
  57. blockDiv.appendChild(imgDiv);
  58. return {
  59. img: img,
  60. imgInput: imgInput,
  61. widthInput: widthInput,
  62. heightInput: heightInput,
  63. imgInputButton: imgInputButton
  64. };
  65. }
  66. function addDataToBlockArray(dataObject, blockArray, blockIndex, dataIndex) {
  67. if ('data' in blockArray[blockIndex]) {
  68. blockArray[blockIndex].data[dataIndex] = dataObject
  69. } else {
  70. blockArray[blockIndex].data = [dataObject];
  71. }
  72. return blockArray;
  73. }
  74. function parseMd(content) {
  75. const parseTitle = line => {
  76. var title = '';
  77. title = line.replace('## **', '');
  78. title = title.replace('**', '');
  79. return title;
  80. };
  81. const parseAmpImg = line => {
  82. if (line.includes('alt')) {
  83. const altParameter = line.replace(/alt=|"/g, '');
  84. return {alt: altParameter};
  85. } else if (line.includes('src')) {
  86. const srcParameter = line.replace(/src=|"/g, '');
  87. return {src: srcParameter};
  88. } else if (line.includes('height')) {
  89. const heightParameter = line.replace(/height=|"/g, '');
  90. return {height: heightParameter};
  91. } else if (line.includes('width')) {
  92. const widthParameter = line.replace(/width=|"/g, '');
  93. return {width: widthParameter};
  94. } else if (line.includes('layout')) {
  95. const layoutParameter = line.replace(/layout=|"|>/g, '');
  96. }
  97. }
  98. var blockArray = [{title: ''}];
  99. var blockCount = 0;
  100. var preDataIndex;
  101. var parseBlockDiv, preBlockindex;
  102. var preImg, preImgObject;
  103. var preImgObject = {'image': {}}
  104. var isNotFrontMatterCount = 0;
  105. var isAmpImgRange = false;
  106. for (const line of content.split('\n')) {
  107. if (isNotFrontMatterCount < 2) {
  108. if (line.includes('---')) {
  109. isNotFrontMatterCount += 1;
  110. }
  111. continue;
  112. }
  113. if (isAmpImgRange === true) {
  114. imgParamObject = parseAmpImg(line);
  115. preImgObject.image = {...preImgObject.image, ...imgParamObject};
  116. const key = _.get(_.keys(imgParamObject), 0);
  117. preImg.setAttribute(key, _.get(imgParamObject, `${key}`));
  118. continue;
  119. }
  120. if (line.includes('##')) {
  121. var preDataIndex = 0;
  122. preBlockindex = blockCount;
  123. blockCount = blockCount + 1;
  124. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
  125. parseBlockDiv = blockDiv;
  126. const title = parseTitle(line);
  127. const text = title;
  128. blockArray[preBlockindex].title = text;
  129. h.textContent = text;
  130. inputButton.onclick = function() {
  131. const text = titleInput.value;
  132. blockArray[preBlockindex].title = text;
  133. h.textContent = text;
  134. }
  135. } else if (line.includes('<amp-img')) {
  136. if (line.includes('</amp-img>')) {
  137. blockArray = addDataToBlockArray(preImgObject, blockArray, preBlockindex, ownDataIndex)
  138. isAmpImgRange = false;
  139. continue;
  140. }
  141. const ownDataIndex = preDataIndex;
  142. preDataIndex += 1;
  143. isAmpImgRange = true;
  144. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(parseBlockDiv);
  145. preImg = img;
  146. imgInputButton.onclick = function() {
  147. preImg.setAttribute('src', imgInput.value);
  148. preImg.setAttribute('width', widthInput.value);
  149. preImg.setAttribute('height', heightInput.value);
  150. preImg.setAttribute('alt', 'image field');
  151. const imgObject = {image: {src: imgInput.value,
  152. height: heightInput.value,
  153. width: widthInput.value,
  154. alt: 'image field',
  155. layout: 'responsive'}};
  156. blockArray = addDataToBlockArray(imgObject, blockArray, preBlockindex, ownDataIndex)
  157. }
  158. } else {
  159. const ownDataIndex = preDataIndex;
  160. preDataIndex += 1;
  161. const {p, descTextArea, descInputButton} = getdescElements(parseBlockDiv);
  162. const text = line;
  163. p.textContent = text;
  164. blockArray = addDataToBlockArray({description: {text: text}}, blockArray, preBlockindex, ownDataIndex)
  165. descInputButton.onclick = function () {
  166. const text = descTextArea.value;
  167. p.textContent = text;
  168. addDataToBlockArray({description: {text: text}}, blockArray, preBlockindex, ownDataIndex)
  169. }
  170. }
  171. }
  172. return {blockArray: blockArray, blockCount: blockCount}
  173. }
  174. const content = `---
  175. title: "公仔與爵士鼓"
  176. date: 2021-02-23T13:18:22+08:00
  177. draft: false
  178. type: "collection"
  179. url: "/collection/doll_and_drum_set"
  180. image: "https://image-cdn-flare.qdm.cloud/q57568769580e2/image/data/2020/11/09/7f6087c94211080bfe2edfb0180891a7.jpg"
  181. description: "很喜歡現代風格居家設計,利用大理石紋路牆面作為客廳的......"
  182. ---
  183. ## **空間主人|公仔控 \- 陳氏夫妻檔**\n\
  184. \n\
  185. 初次見到陳先生,其實在心裡偷偷覺得,他好像有點嚴肅、不太笑,跟我們打個招呼後,就趕著出門上課了,但實際上多相處後才發現,「外冷內熱」比較貼近真實的他。
  186. <amp-img
  187. alt="小寶優居"
  188. src="https://image-cdn-flare.qdm.cloud/q57568769580e2/image/data/2020/11/10/7200f7c668635a2ef399723c5410ca90.jpg"
  189. height="340"
  190. width="604"
  191. layout="responsive">
  192. </amp-img>
  193. `
  194. const contentDiv = document.getElementsByClassName('ui segment')[0];
  195. const titleButton = document.getElementById('title_button');
  196. const submitButton = document.getElementById('submit_button');
  197. var {blockArray, blockCount} = parseMd(content);
  198. var dataIndex;
  199. titleButton.onclick = function() {
  200. dataIndex = 0;
  201. const blockIndex = blockCount;
  202. blockCount = blockCount + 1;
  203. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
  204. inputButton.onclick = function() {
  205. const text = titleInput.value;
  206. if (blockIndex < blockArray.length) {
  207. blockArray[blockIndex].title = text;
  208. h.textContent = text;
  209. } else {
  210. blockArray[blockIndex] = {title: text};
  211. h.textContent = text;
  212. }
  213. }
  214. descButton.onclick = function() {
  215. const ownDataIndex = dataIndex;
  216. dataIndex += 1;
  217. const {p, descTextArea, descInputButton} = getdescElements(blockDiv);
  218. descInputButton.onclick = function() {
  219. const text = descTextArea.value;
  220. p.textContent = text;
  221. addDataToBlockArray({description: {text: text}}, blockArray, blockIndex, ownDataIndex)
  222. }
  223. }
  224. imgButton.onclick = function() {
  225. const ownDataIndex = dataIndex;
  226. dataIndex += 1;
  227. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(blockDiv);
  228. imgInputButton.onclick = function() {
  229. img.setAttribute('src', imgInput.value);
  230. img.setAttribute('width', widthInput.value);
  231. img.setAttribute('height', heightInput.value);
  232. img.setAttribute('alt', 'image field');
  233. const imgObject = {image: {src: imgInput.value,
  234. height: heightInput.value,
  235. width: widthInput.value,
  236. alt: 'image field',
  237. layout: 'responsive'}};
  238. blockArray = addDataToBlockArray(imgObject, blockArray, blockIndex, ownDataIndex)
  239. }
  240. }
  241. }
  242. submitButton.onclick = function() {
  243. var mdContent = '';
  244. for (var idx=0; idx<blockArray.length; idx++) {
  245. mdContent += `## **${_.get(blockArray[idx], 'title', '')}**\n`
  246. for (var data of _.get(blockArray[idx], 'data', [])) {
  247. if (_.get(_.keys(data), 0) === 'description') {
  248. mdContent += _.get(data, 'description.text', '') + '\n';
  249. } else if (_.get(_.keys(data), 0) === 'image') {
  250. ampImgForm = `<amp-img\
  251. \n alt="${_.get(data, 'image.alt', '小寶優居')}"\
  252. \n src="${_.get(data, 'image.src', '')}"\
  253. \n height="${_.get(data, 'image.height', 300)}"\
  254. \n width="${_.get(data, 'image.width', 400)}"\
  255. \n layout="${_.get(data, 'image.layout', 'responsive')}>"\
  256. \n</amp-img>`;
  257. mdContent += ampImgForm;
  258. }
  259. }
  260. }
  261. console.log(mdContent);
  262. console.log(blockArray);
  263. }