markdown格式转html格式

76 阅读1分钟

const parseContent = (content: string) => { const lines = content.split('\n'); let maxLength = 0 return lines.map((line,index) => { const match = line.match(/#/g); const hr = line.match(/---/) const bold = line.match(/**/g) const pipe = line.match(/|/g) if (match) { const level = match.length; if(bold&&bold.length===2){ const data = line.replaceAll('#','')
const boldMatch = data.replace(/**(.*?)**/g, '1</strong>);return<h1</strong>'); return `<h{level}>boldMatch</h{boldMatch}</h{level}>; }else{ const data = line.replaceAll('#','') const text = data; return <hlevel>{level}>{text}</h{level}>`; } }else if(hr){ return `<hr/>` }else if(bold){ if(bold.length===2){ const boldMatch = line.replace(/\*\*(.*?)\*\*/g, '<strong>1'); return <p>${boldMatch}</p>; // 普通段落 } }else if(pipe){ let maxPipePositions = []; const pipevalues = line.split('|') const values = pipevalues.slice(1,4) let res = '' let i = 0 while(true){ if(/|/.test(lines[index+i])){ const nextLine = lines[index+i].split('|') const nextArr = nextLine.slice(1,4) nextArr.forEach((item,index) => { maxLength = Math.max(maxLength, item.length) }) i++ }else{ break } } values.forEach(item => { res += (item.trim() + ' '.repeat(maxLength)) }) return <p>${res}</p> } return <p>${line}</p>; }).join(''); };

const parsedContent = computed(() => { return parseContent(selectedTopic.value.content).replaceAll('-','') });