function parseMd(content) { var blockArray = [{title: ''}]; var blockCount; var preDataIndex, imgDataIndex; var parseBlockDiv; var preImg, preImgObject; var preImgObject = {'image': {}} var isNotFrontMatterCount = 0; var isAmpImgRange = false; for (const line of content.split('\n')) { if (isNotFrontMatterCount < 2) { if (line.includes('---')) { isNotFrontMatterCount += 1; } continue; } if (isAmpImgRange === true && !(line.includes(''))) { imgParamObject = parseAmpImg(line); preImgObject.image = {...preImgObject.image, ...imgParamObject}; const key = _.get(_.keys(imgParamObject), 0); preImg.setAttribute(key, _.get(imgParamObject, `${key}`)); continue; } if (line.includes('##')) { preDataIndex = 0; blockCount = blockCount + 1 | 0; const preBlockindex = blockCount; const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements(contentDiv); parseBlockDiv = blockDiv; const title = parseTitle(line); blockArray[preBlockindex] = {title: title}; h.textContent = title; inputButton.onclick = function() { const title = titleInput.value; blockArray[preBlockindex].title = title; h.textContent = title; } descButton.onclick = function() { const ownDataIndex = blockArray[preBlockindex].data.length; const {p, descTextArea, descInputButton} = getdescElements(blockDiv); descInputButton.onclick = function() { blockArray = handleDescInputClick(p, descTextArea.value, blockArray, preBlockindex, ownDataIndex); } } imgButton.onclick = function() { const imgDataIndex = blockArray[preBlockindex].data.length; const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(blockDiv); imgInputButton.onclick = function() { const imgObject = {image: {src: imgInput.value, height: heightInput.value, width: widthInput.value, alt: 'image field', layout: 'responsive'}}; blockArray = handleImgInputClick(img, imgObject, blockArray, preBlockindex, imgDataIndex); } } } else if (line.includes('amp-img')) { const preBlockindex = blockCount; if (line.includes('')) { blockArray = addDataToBlockArray(preImgObject, blockArray, preBlockindex, imgDataIndex); isAmpImgRange = false; continue; } imgDataIndex = preDataIndex; preDataIndex += 1; isAmpImgRange = true; const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(parseBlockDiv); preImg = img; imgInputButton.onclick = function() { const imgObject = {image: {src: imgInput.value, height: heightInput.value, width: widthInput.value, alt: 'image field', layout: 'responsive'}}; blockArray = handleImgInputClick(img, imgObject, blockArray, preBlockindex, imgDataIndex); } } else { const preBlockindex = blockCount; const ownDataIndex = preDataIndex; preDataIndex += 1; const {p, descTextArea, descInputButton} = getdescElements(parseBlockDiv); const text = line; p.textContent = text; blockArray = addDataToBlockArray({description: {text: text}}, blockArray, preBlockindex, ownDataIndex); descInputButton.onclick = function() { blockArray = handleDescInputClick(p, descTextArea.value, blockArray, preBlockindex, ownDataIndex); } } } return {blockArray: blockArray, blockCount: blockCount} } const parseTitle = line => { var title = ''; title = line.replace('## **', ''); title = title.replace('**', ''); return title; }; const parseAmpImg = line => { if (line.includes('alt')) { const altParameter = line.replace(/alt=|"/g, ''); return {alt: altParameter}; } else if (line.includes('src')) { const srcParameter = line.replace(/src=|"/g, ''); return {src: srcParameter}; } else if (line.includes('height')) { const heightParameter = line.replace(/height=|"/g, ''); return {height: heightParameter}; } else if (line.includes('width')) { const widthParameter = line.replace(/width=|"/g, ''); return {width: widthParameter}; } else if (line.includes('layout')) { const layoutParameter = line.replace(/layout=|"|>/g, ''); } } function handleDescInputClick(p, desc, blockArray, blockIndex, ownDataIndex) { p.textContent = desc; return addDataToBlockArray({description: {text: desc}}, blockArray, blockIndex, ownDataIndex) } function handleImgInputClick(img, imgObject, blockArray, blockIndex, ownDataIndex) { img.setAttribute('src', _.get(imgObject, 'image.src')); img.setAttribute('width', _.get(imgObject, 'image.width')); img.setAttribute('height', _.get(imgObject, 'image.height')); img.setAttribute('alt', _.get(imgObject, 'image.alt')); return addDataToBlockArray(imgObject, blockArray, blockIndex, ownDataIndex) }