Skip to content

Commit

Permalink
[Issue #23] Implement '**' to <strong>, '*' to <em>, '***' to <strong…
Browse files Browse the repository at this point in the history
…><em>.
  • Loading branch information
SeeChen committed Dec 17, 2024
1 parent 9866fd7 commit ce1efd4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 103 deletions.
13 changes: 13 additions & 0 deletions JavaScript/About/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ const SeeChen_AboutPage_AboutSites = {
eieieiei [seechen.github.io](#) is
<table>
<tr>
<td>AAA</td>
</tr>
</table>
|a|a|a|
|:--:|:--:|:--:|
|a|a|a|
> *a*
> [a](#)
> # 这是一个引用
>> 这是一个子引用`
window.myData.about.contentExpand.children[1].children = window.md2vDom.convert(test_md);
Expand Down
130 changes: 27 additions & 103 deletions JavaScript/General/md2vDom.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
For more details, see <https://www.gnu.org/licenses/>.
*/


export const Markdown2vDom = {

// Markdown rules reference https://www.markdownguide.org/basic-syntax/
Expand All @@ -33,124 +32,49 @@ export const Markdown2vDom = {
const vDomObj = [];

const lines = markdown.split("\n");
let inParagraph = false;

for(let line of lines) {
let vDom_Children = {};
let isPush = true;

line.trim();
for (let i = 0; i < lines.length; i++) {

let vDomChildren = {}
let line = lines[i].trim();

line = line.replace(/\*\*\*(.*?)\*\*\*/gim, '<strong><em>$1</em></strong>');
line = line.replace(/\*\*(.*?)\*\*/gim, '<strong>$1</strong>');
line = line.replace(/\*(.*?)\*/gim, '<em>$1</em>');
line = line.replace(/\*\*\*(.*?)\*\*\*/gim, '<strong><em>$1</em></strong>')
.replace(/\*\*(.*?)\*\*/gim, '<strong>$1</strong>')
.replace(/\*(.*?)\*/gim, '<em>$1</em>')

line = line.replace(/\[(.*?)\]\((.*?)\)/gim, '<a href="$2" target="_blank">$1</a>');
.replace(/\[(.*?)\]\((.*?)\)/gim, '<a href="$2" target="_blank">$1</a>');

if (line.startsWith(">> ")) {

line = line.replace(">> ", "");
} else if (line.startsWith("> ")) {
line = line.replace("> ", "");
}

if (/^###### (.*)/.test(line)) {
inParagraph = false;
if (line.startsWith(">> ") || line.startsWith("> ")) {

var realContent = Markdown2vDom.getRealContent(line.replace(/^###### /, ''));

vDomChildren = {
tag: "h6",
line.replace("> ", "");
vDom_Children = {
tag: "blockquote",
props: {},
lang: realContent.lang,
children: [realContent.content]
lang: "",
children: []
};
} else if (/^##### (.*)/.test(line)) {
inParagraph = false;

var realContent = Markdown2vDom.getRealContent(line.replace(/^##### /, ''));

vDomChildren = {
tag: "h5",
props: {},
lang: realContent.lang,
children: [realContent.content]
};
} else if (/^#### (.*)/.test(line)) {
inParagraph = false;

var realContent = Markdown2vDom.getRealContent(line.replace(/^#### /, ''));

vDomChildren = {
tag: "h4",
props: {},
lang: realContent.lang,
children: [realContent.content]
};
} else if (/^### (.*)/.test(line)) {
inParagraph = false;

var realContent = Markdown2vDom.getRealContent(line.replace(/^### /, ''));

vDomChildren = {
tag: "h3",
props: {},
lang: realContent.lang,
children: [realContent.content]
};
} else if (/^## (.*)/.test(line)) {
inParagraph = false;

var realContent = Markdown2vDom.getRealContent(line.replace(/^## /, ''));

vDomChildren = {
tag: "h2",
props: {},
lang: realContent.lang,
children: [realContent.content]
};
} else if (/^# (.*)/.test(line)) {
inParagraph = false;

var realContent = Markdown2vDom.getRealContent(line.replace(/^# /, ''));

vDomChildren = {
tag: "h1",
props: {},
lang: realContent.lang,
children: [realContent.content]
};
}

else if (line.length === 0) {
inParagraph = false;
} else if (line.length > 0) {

if (inParagraph) {

const lastDom = vDomObj[vDomObj.length - 1];
if (lastDom.tag === "p") {
lastDom.children = [`${lastDom.children[0]}<br>${line}`]
}
if (i < lines.length - 1 && lines[i + 1].trim().startsWith("> ")) {
isPush = false;
} else {
isPush = true;
}
}

inParagraph = true;
if (/^###### (.*)/.test(line)) {
line = line.replace(/^###### /, "");

var realContent = Markdown2vDom.getRealContent(line);
if (isPush) {

vDomChildren = {
tag: "p",
props: {
style: "color: red;"
},
lang: realContent.lang,
children: [realContent.content]
};
}
}

if (Object.keys(vDomChildren).length > 0) {
vDomObj.push(vDomChildren);
console.log(vDom_Children);

if (isPush && Object.keys(vDom_Children).length > 0) {
vDomObj.push(vDom_Children);
vDom_Children = {}
}
}

Expand Down

0 comments on commit ce1efd4

Please sign in to comment.