function getBlockElements() {
const blockDiv = document.createElement("div");
const descButton = document.createElement('button');
descButton.textContent = 'Add Description';
const imgButton = document.createElement('button');
imgButton.textContent = 'Add Image';
const h = document.createElement("H2");
const input = document.createElement("INPUT");
input.setAttribute("type", "text");
const inputButton = document.createElement('button')
inputButton.textContent = 'Title新增/修改';
contentDiv.append(blockDiv);
blockDiv.appendChild(h);
blockDiv.appendChild(input);
blockDiv.appendChild(inputButton);
blockDiv.appendChild(descButton);
blockDiv.appendChild(imgButton);
return {
blockDiv: blockDiv,
h: h,
titleInput: input,
inputButton: inputButton,
descButton: descButton,
imgButton: imgButton
};
}
function getdescElements(blockDiv){
const descDiv = document.createElement("div");
const p = document.createElement("p");
const descTextArea = document.createElement("TEXTAREA");
descTextArea.setAttribute("type", "text");
const descInputButton = document.createElement('button')
descInputButton.textContent = 'Description新增/修改';
descDiv.appendChild(p);
descDiv.appendChild(descTextArea);
descDiv.appendChild(descInputButton);
blockDiv.appendChild(descDiv);
return {
p: p,
descTextArea: descTextArea,
descInputButton: descInputButton
};
}
function getImgElements(blockDiv) {
const imgDiv = document.createElement("div");
const img = document.createElement("img");
const imgInput = document.createElement("INPUT");
const widthInput = document.createElement("INPUT");
const heightInput = document.createElement("INPUT");
const imgInputButton = document.createElement('button');
imgInputButton.textContent = 'Image新增/修改';
imgDiv.appendChild(img);
imgDiv.appendChild(imgInput);
imgDiv.appendChild(widthInput);
imgDiv.appendChild(heightInput);
imgDiv.appendChild(imgInputButton);
blockDiv.appendChild(imgDiv);
return {
img: img,
imgInput: imgInput,
widthInput: widthInput,
heightInput: heightInput,
imgInputButton: imgInputButton
};
}
function addDataToBlockArray(dataObject, blockArray, index) {
if ('data' in blockArray[index]) {
blockArray[index].data.push(dataObject)
} else {
blockArray[index].data = [dataObject];
}
return blockArray;
}
function parseMd(content) {
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, '');
}
}
var blockArray = [];
var blockCount = 0;
var parseBlockDiv, index;
var pre_img, pre_imgObject;
var isNotFrontMatterCount = 0;
var isAmpImgRange = false;
for (const line of content.split('\n')) {
if (isNotFrontMatterCount < 2) {
if (line.includes('---')) {
isNotFrontMatterCount += 1;
}
continue;
}
if (line.includes('##')) {
index = blockCount;
blockCount = blockCount + 1;
const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
parseBlockDiv = blockDiv;
const title = parseTitle(line);
const text = title;
blockArray[index] = {'title': text};
h.textContent = text;
inputButton.onclick = function() {
const text = titleInput.value;
blockArray[index] = {'title': text};
h.textContent = text;
}
} else if (isAmpImgRange === true) {
if (line.includes('')) {
blockArray = addDataToBlockArray({'image': pre_imgObject}, blockArray, index)
isAmpImgRange = false;
} else {
imgParamObject = parseAmpImg(line);
const key = _.get(_.keys(imgParamObject), 0);
pre_img.setAttribute(key, _.get(imgParamObject, `${key}`));
}
} else if (line.includes('
`
const contentDiv = document.getElementsByClassName('ui segment')[0];
const titleButton = document.getElementById('title_button');
const submitButton = document.getElementById('submit_button');
var {blockArray, blockCount} = parseMd(content);
titleButton.onclick = function() {
const index = blockCount;
blockCount = blockCount + 1;
const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
inputButton.onclick = function() {
const text = titleInput.value;
blockArray[index] = {'title': text};
h.textContent = text;
}
descButton.onclick = function() {
const {p, descTextArea, descInputButton} = getdescElements(blockDiv);
descInputButton.onclick = function() {
const text = descTextArea.value;
p.textContent = text;
addDataToBlockArray({description: {text: text}}, blockArray, index)
}
}
imgButton.onclick = function() {
const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(blockDiv);
imgInputButton.onclick = function() {
img.setAttribute('src', imgInput.value);
img.setAttribute('width', widthInput.value);
img.setAttribute('height', heightInput.value);
img.setAttribute('alt', 'image field');
const imgObject = {image: {src: imgInput.value,
height: heightInput.value,
width: widthInput.value,
alt: 'image field',
layout: 'responsive'}};
blockArray = addDataToBlockArray(imgObject, blockArray, index)
}
}
}
submitButton.onclick = function() {
var mdContent = '';
for (var idx=0; idx`;
mdContent += ampImgForm;
}
}
}
console.log(mdContent);
console.log(blockArray);
}