Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
eaf89aa2
Unverified
Commit
eaf89aa2
authored
Oct 04, 2022
by
boojack
Committed by
GitHub
Oct 04, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: update marked parsers (#260)
* chore: remove external match functions * chore: update parsers
parent
4bd373ba
Changes
18
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
88 additions
and
173 deletions
+88
-173
index.ts
web/src/labs/marked/index.ts
+38
-6
marked.test.ts
web/src/labs/marked/marked.test.ts
+24
-0
Bold.ts
web/src/labs/marked/parser/Bold.ts
+9
-11
CodeBlock.ts
web/src/labs/marked/parser/CodeBlock.ts
+0
-11
DoneList.ts
web/src/labs/marked/parser/DoneList.ts
+1
-12
Emphasis.ts
web/src/labs/marked/parser/Emphasis.ts
+9
-11
Image.ts
web/src/labs/marked/parser/Image.ts
+0
-11
InlineCode.ts
web/src/labs/marked/parser/InlineCode.ts
+0
-11
Link.ts
web/src/labs/marked/parser/Link.ts
+0
-11
Mark.ts
web/src/labs/marked/parser/Mark.ts
+0
-11
OrderedList.ts
web/src/labs/marked/parser/OrderedList.ts
+1
-12
Paragraph.ts
web/src/labs/marked/parser/Paragraph.ts
+1
-12
PlainLink.ts
web/src/labs/marked/parser/PlainLink.ts
+0
-11
Tag.ts
web/src/labs/marked/parser/Tag.ts
+0
-11
TodoList.ts
web/src/labs/marked/parser/TodoList.ts
+1
-12
UnorderedList.ts
web/src/labs/marked/parser/UnorderedList.ts
+1
-12
index.ts
web/src/labs/marked/parser/index.ts
+1
-6
memo-content.less
web/src/less/memo-content.less
+2
-2
No files found.
web/src/labs/marked/index.ts
View file @
eaf89aa2
import
{
p
arserList
}
from
"./parser"
;
import
{
blockElementParserList
,
inlineElementP
arserList
}
from
"./parser"
;
export
const
marked
=
(
markdownStr
:
string
,
parsers
=
parserList
)
=>
{
const
match
=
(
rawStr
:
string
,
regex
:
RegExp
):
number
=>
{
for
(
const
parser
of
parsers
)
{
const
matchResult
=
rawStr
.
match
(
regex
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
export
const
marked
=
(
markdownStr
:
string
,
blockParsers
=
blockElementParserList
,
inlineParsers
=
inlineElementParserList
):
string
=>
{
for
(
const
parser
of
blockParsers
)
{
const
startIndex
=
markdownStr
.
search
(
parser
.
regex
);
const
startIndex
=
markdownStr
.
search
(
parser
.
regex
);
const
matchedLength
=
parser
.
match
(
markdownStr
);
const
matchedLength
=
match
(
markdownStr
,
parser
.
regex
);
if
(
startIndex
>
-
1
&&
matchedLength
>
0
)
{
if
(
startIndex
>
-
1
&&
matchedLength
>
0
)
{
const
prefixStr
=
markdownStr
.
slice
(
0
,
startIndex
);
const
prefixStr
=
markdownStr
.
slice
(
0
,
startIndex
);
const
matchedStr
=
markdownStr
.
slice
(
startIndex
,
startIndex
+
matchedLength
);
const
matchedStr
=
markdownStr
.
slice
(
startIndex
,
startIndex
+
matchedLength
);
const
suffixStr
=
markdownStr
.
slice
(
startIndex
+
matchedLength
);
const
suffixStr
=
markdownStr
.
slice
(
startIndex
+
matchedLength
);
markdownStr
=
marked
(
prefixStr
,
parsers
)
+
parser
.
renderer
(
matchedStr
)
+
marked
(
suffixStr
,
parsers
);
return
marked
(
prefixStr
,
blockParsers
,
inlineParsers
)
+
parser
.
renderer
(
matchedStr
)
+
marked
(
suffixStr
,
blockParsers
,
inlineParsers
);
break
;
}
}
let
matchedInlineParser
=
undefined
;
let
matchedIndex
=
-
1
;
for
(
const
parser
of
inlineElementParserList
)
{
const
startIndex
=
markdownStr
.
search
(
parser
.
regex
);
const
matchedLength
=
match
(
markdownStr
,
parser
.
regex
);
if
(
startIndex
>
-
1
&&
matchedLength
>
0
)
{
if
(
!
matchedInlineParser
||
matchedIndex
>
startIndex
)
{
matchedIndex
=
startIndex
;
matchedInlineParser
=
parser
;
}
}
}
}
}
if
(
matchedInlineParser
)
{
const
matchedLength
=
match
(
markdownStr
,
matchedInlineParser
.
regex
);
const
prefixStr
=
markdownStr
.
slice
(
0
,
matchedIndex
);
const
matchedStr
=
markdownStr
.
slice
(
matchedIndex
,
matchedIndex
+
matchedLength
);
const
suffixStr
=
markdownStr
.
slice
(
matchedIndex
+
matchedLength
);
return
prefixStr
+
matchedInlineParser
.
renderer
(
matchedStr
)
+
marked
(
suffixStr
,
[],
inlineParsers
);
}
return
markdownStr
;
return
markdownStr
;
};
};
web/src/labs/marked/marked.test.ts
View file @
eaf89aa2
...
@@ -94,6 +94,30 @@ console.log("hello world!")
...
@@ -94,6 +94,30 @@ console.log("hello world!")
},
},
];
];
for
(
const
t
of
tests
)
{
expect
(
marked
(
t
.
markdown
)).
toBe
(
t
.
want
);
}
});
test
(
"parse bold and em text"
,
()
=>
{
const
tests
=
[
{
markdown
:
`Important: **Minecraft**`
,
want
:
`<p>Important: <strong>Minecraft</strong></p>`
,
},
{
markdown
:
`Em: *Minecraft*`
,
want
:
`<p>Em: <em>Minecraft</em></p>`
,
},
{
markdown
:
`Important: ***Minecraft/123***`
,
want
:
`<p>Important: <strong><em>Minecraft/123</em></strong></p>`
,
},
{
markdown
:
`Important: ***[baidu](https://baidu.com)***`
,
want
:
`<p>Important: <strong><em><a class='link' target='_blank' rel='noreferrer' href='https://baidu.com'>baidu</a></em></strong></p>`
,
},
];
for
(
const
t
of
tests
)
{
for
(
const
t
of
tests
)
{
expect
(
marked
(
t
.
markdown
)).
toBe
(
t
.
want
);
expect
(
marked
(
t
.
markdown
)).
toBe
(
t
.
want
);
}
}
...
...
web/src/labs/marked/parser/Bold.ts
View file @
eaf89aa2
export
const
BOLD_REG
=
/
\*\*([\S
]
+
?)\*\*
/
;
import
{
marked
}
from
".."
;
import
Emphasis
from
"./Emphasis"
;
import
Link
from
"./Link"
;
const
match
=
(
rawStr
:
string
):
number
=>
{
export
const
BOLD_REG
=
/
\*\*([\S
]
+
)\*\*
/
;
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
matchResult
=
rawStr
.
match
(
BOLD_REG
);
const
matchResult
=
rawStr
.
match
(
BOLD_REG
);
if
(
!
matchResult
)
{
if
(
!
matchResult
)
{
return
0
;
return
rawStr
;
}
}
const
matchStr
=
matchResult
[
0
];
const
parsedContent
=
marked
(
matchResult
[
1
],
[],
[
Emphasis
,
Link
]);
return
matchStr
.
length
;
return
`<strong>
${
parsedContent
}
</strong>`
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
BOLD_REG
,
"<strong>$1</strong>"
);
return
parsedStr
;
};
};
export
default
{
export
default
{
name
:
"bold"
,
name
:
"bold"
,
regex
:
BOLD_REG
,
regex
:
BOLD_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/CodeBlock.ts
View file @
eaf89aa2
export
const
CODE_BLOCK_REG
=
/^```
(\S
*
?)\s([\s\S]
*
?)
```
(\n?)
/
;
export
const
CODE_BLOCK_REG
=
/^```
(\S
*
?)\s([\s\S]
*
?)
```
(\n?)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
CODE_BLOCK_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
CODE_BLOCK_REG
,
"<pre lang='$1'>
\n
$2</pre>$3"
);
const
parsedStr
=
rawStr
.
replace
(
CODE_BLOCK_REG
,
"<pre lang='$1'>
\n
$2</pre>$3"
);
return
parsedStr
;
return
parsedStr
;
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
export
default
{
export
default
{
name
:
"code block"
,
name
:
"code block"
,
regex
:
CODE_BLOCK_REG
,
regex
:
CODE_BLOCK_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/DoneList.ts
View file @
eaf89aa2
...
@@ -3,29 +3,18 @@ import { marked } from "..";
...
@@ -3,29 +3,18 @@ import { marked } from "..";
export
const
DONE_LIST_REG
=
/^-
\[
x
\]
([\S
]
+
)(\n?)
/
;
export
const
DONE_LIST_REG
=
/^-
\[
x
\]
([\S
]
+
)(\n?)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
DONE_LIST_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
matchResult
=
rawStr
.
match
(
DONE_LIST_REG
);
const
matchResult
=
rawStr
.
match
(
DONE_LIST_REG
);
if
(
!
matchResult
)
{
if
(
!
matchResult
)
{
return
rawStr
;
return
rawStr
;
}
}
const
parsedContent
=
marked
(
matchResult
[
1
],
inlineElementParserList
);
const
parsedContent
=
marked
(
matchResult
[
1
],
[],
inlineElementParserList
);
return
`<p><span class='todo-block done' data-value='DONE'>✓</span>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
return
`<p><span class='todo-block done' data-value='DONE'>✓</span>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
};
};
export
default
{
export
default
{
name
:
"done list"
,
name
:
"done list"
,
regex
:
DONE_LIST_REG
,
regex
:
DONE_LIST_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/Emphasis.ts
View file @
eaf89aa2
export
const
EMPHASIS_REG
=
/
\*([\S
]
+
?)\*
/
;
import
{
marked
}
from
".."
;
import
Bold
from
"./Bold"
;
import
Link
from
"./Link"
;
const
match
=
(
rawStr
:
string
):
number
=>
{
export
const
EMPHASIS_REG
=
/
\*([\S
]
+
)\*
/
;
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
matchResult
=
rawStr
.
match
(
EMPHASIS_REG
);
const
matchResult
=
rawStr
.
match
(
EMPHASIS_REG
);
if
(
!
matchResult
)
{
if
(
!
matchResult
)
{
return
0
;
return
rawStr
;
}
}
const
matchStr
=
matchResult
[
0
];
const
parsedContent
=
marked
(
matchResult
[
1
],
[],
[
Bold
,
Link
]);
return
matchStr
.
length
;
return
`<em>
${
parsedContent
}
</em>`
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
EMPHASIS_REG
,
"<em>$1</em>"
);
return
parsedStr
;
};
};
export
default
{
export
default
{
name
:
"emphasis"
,
name
:
"emphasis"
,
regex
:
EMPHASIS_REG
,
regex
:
EMPHASIS_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/Image.ts
View file @
eaf89aa2
export
const
IMAGE_REG
=
/!
\[
.*
?\]\((
.+
?)\)
/
;
export
const
IMAGE_REG
=
/!
\[
.*
?\]\((
.+
?)\)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
IMAGE_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
IMAGE_REG
,
"<img class='img' src='$1' />"
);
const
parsedStr
=
rawStr
.
replace
(
IMAGE_REG
,
"<img class='img' src='$1' />"
);
return
parsedStr
;
return
parsedStr
;
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
export
default
{
export
default
{
name
:
"image"
,
name
:
"image"
,
regex
:
IMAGE_REG
,
regex
:
IMAGE_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/InlineCode.ts
View file @
eaf89aa2
export
const
INLINE_CODE_REG
=
/`
([\S
]
+
?)
`/
;
export
const
INLINE_CODE_REG
=
/`
([\S
]
+
?)
`/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
INLINE_CODE_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
INLINE_CODE_REG
,
"<code>$1</code>"
);
const
parsedStr
=
rawStr
.
replace
(
INLINE_CODE_REG
,
"<code>$1</code>"
);
return
parsedStr
;
return
parsedStr
;
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
export
default
{
export
default
{
name
:
"inline code"
,
name
:
"inline code"
,
regex
:
INLINE_CODE_REG
,
regex
:
INLINE_CODE_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/Link.ts
View file @
eaf89aa2
export
const
LINK_REG
=
/
\[(
.*
?)\]\((
.+
?)\)
/
;
export
const
LINK_REG
=
/
\[(
.*
?)\]\((
.+
?)\)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
LINK_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
LINK_REG
,
"<a class='link' target='_blank' rel='noreferrer' href='$2'>$1</a>"
);
const
parsedStr
=
rawStr
.
replace
(
LINK_REG
,
"<a class='link' target='_blank' rel='noreferrer' href='$2'>$1</a>"
);
return
parsedStr
;
return
parsedStr
;
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
export
default
{
export
default
{
name
:
"link"
,
name
:
"link"
,
regex
:
LINK_REG
,
regex
:
LINK_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/Mark.ts
View file @
eaf89aa2
export
const
MARK_REG
=
/@
\[([\S
]
+
?)\]\((\S
+
?)\)
/
;
export
const
MARK_REG
=
/@
\[([\S
]
+
?)\]\((\S
+
?)\)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
MARK_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
MARK_REG
,
"<span class='memo-link-text' data-value='$2'>$1</span>"
);
const
parsedStr
=
rawStr
.
replace
(
MARK_REG
,
"<span class='memo-link-text' data-value='$2'>$1</span>"
);
return
parsedStr
;
return
parsedStr
;
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
export
default
{
export
default
{
name
:
"mark"
,
name
:
"mark"
,
regex
:
MARK_REG
,
regex
:
MARK_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/OrderedList.ts
View file @
eaf89aa2
...
@@ -3,29 +3,18 @@ import { marked } from "..";
...
@@ -3,29 +3,18 @@ import { marked } from "..";
export
const
ORDERED_LIST_REG
=
/^
(\d
+
)\.
([\S
]
+
)(\n?)
/
;
export
const
ORDERED_LIST_REG
=
/^
(\d
+
)\.
([\S
]
+
)(\n?)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
ORDERED_LIST_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
matchResult
=
rawStr
.
match
(
ORDERED_LIST_REG
);
const
matchResult
=
rawStr
.
match
(
ORDERED_LIST_REG
);
if
(
!
matchResult
)
{
if
(
!
matchResult
)
{
return
rawStr
;
return
rawStr
;
}
}
const
parsedContent
=
marked
(
matchResult
[
2
],
inlineElementParserList
);
const
parsedContent
=
marked
(
matchResult
[
2
],
[],
inlineElementParserList
);
return
`<p><span class='ol-block'>
${
matchResult
[
1
]}
.</span>
${
parsedContent
}
</p>
${
matchResult
[
3
]}
`
;
return
`<p><span class='ol-block'>
${
matchResult
[
1
]}
.</span>
${
parsedContent
}
</p>
${
matchResult
[
3
]}
`
;
};
};
export
default
{
export
default
{
name
:
"ordered list"
,
name
:
"ordered list"
,
regex
:
ORDERED_LIST_REG
,
regex
:
ORDERED_LIST_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/Paragraph.ts
View file @
eaf89aa2
...
@@ -3,29 +3,18 @@ import { marked } from "..";
...
@@ -3,29 +3,18 @@ import { marked } from "..";
export
const
PARAGRAPH_REG
=
/^
([\S
]
*
)(\n?)
/
;
export
const
PARAGRAPH_REG
=
/^
([\S
]
*
)(\n?)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
PARAGRAPH_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
matchResult
=
rawStr
.
match
(
PARAGRAPH_REG
);
const
matchResult
=
rawStr
.
match
(
PARAGRAPH_REG
);
if
(
!
matchResult
)
{
if
(
!
matchResult
)
{
return
rawStr
;
return
rawStr
;
}
}
const
parsedContent
=
marked
(
matchResult
[
1
],
inlineElementParserList
);
const
parsedContent
=
marked
(
matchResult
[
1
],
[],
inlineElementParserList
);
return
`<p>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
return
`<p>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
};
};
export
default
{
export
default
{
name
:
"ordered list"
,
name
:
"ordered list"
,
regex
:
PARAGRAPH_REG
,
regex
:
PARAGRAPH_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/PlainLink.ts
View file @
eaf89aa2
export
const
PLAIN_LINK_REG
=
/
(
https
?
:
\/\/[^
]
+
)
/
;
export
const
PLAIN_LINK_REG
=
/
(
https
?
:
\/\/[^
]
+
)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
PLAIN_LINK_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
PLAIN_LINK_REG
,
"<a class='link' target='_blank' rel='noreferrer' href='$1'>$1</a>"
);
const
parsedStr
=
rawStr
.
replace
(
PLAIN_LINK_REG
,
"<a class='link' target='_blank' rel='noreferrer' href='$1'>$1</a>"
);
return
parsedStr
;
return
parsedStr
;
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
export
default
{
export
default
{
name
:
"plain link"
,
name
:
"plain link"
,
regex
:
PLAIN_LINK_REG
,
regex
:
PLAIN_LINK_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/Tag.ts
View file @
eaf89aa2
export
const
TAG_REG
=
/
[^\s]?
#
([^\s
#
]
+
?)
/
;
export
const
TAG_REG
=
/
[^\s]?
#
([^\s
#
]
+
?)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
TAG_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
parsedStr
=
rawStr
.
replace
(
TAG_REG
,
"<span class='tag-span'>#$1</span> "
);
const
parsedStr
=
rawStr
.
replace
(
TAG_REG
,
"<span class='tag-span'>#$1</span> "
);
return
parsedStr
;
return
parsedStr
;
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
...
@@ -18,6 +8,5 @@ const renderer = (rawStr: string): string => {
export
default
{
export
default
{
name
:
"tag"
,
name
:
"tag"
,
regex
:
TAG_REG
,
regex
:
TAG_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/TodoList.ts
View file @
eaf89aa2
...
@@ -3,29 +3,18 @@ import { marked } from "..";
...
@@ -3,29 +3,18 @@ import { marked } from "..";
export
const
TODO_LIST_REG
=
/^-
\[
\]
([\S
]
+
)(\n?)
/
;
export
const
TODO_LIST_REG
=
/^-
\[
\]
([\S
]
+
)(\n?)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
TODO_LIST_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
matchResult
=
rawStr
.
match
(
TODO_LIST_REG
);
const
matchResult
=
rawStr
.
match
(
TODO_LIST_REG
);
if
(
!
matchResult
)
{
if
(
!
matchResult
)
{
return
rawStr
;
return
rawStr
;
}
}
const
parsedContent
=
marked
(
matchResult
[
1
],
inlineElementParserList
);
const
parsedContent
=
marked
(
matchResult
[
1
],
[],
inlineElementParserList
);
return
`<p><span class='todo-block todo' data-value='TODO'></span>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
return
`<p><span class='todo-block todo' data-value='TODO'></span>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
};
};
export
default
{
export
default
{
name
:
"todo list"
,
name
:
"todo list"
,
regex
:
TODO_LIST_REG
,
regex
:
TODO_LIST_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/UnorderedList.ts
View file @
eaf89aa2
...
@@ -3,29 +3,18 @@ import { marked } from "..";
...
@@ -3,29 +3,18 @@ import { marked } from "..";
export
const
UNORDERED_LIST_REG
=
/^
[
*-
]
([\S
]
+
)(\n?)
/
;
export
const
UNORDERED_LIST_REG
=
/^
[
*-
]
([\S
]
+
)(\n?)
/
;
const
match
=
(
rawStr
:
string
):
number
=>
{
const
matchResult
=
rawStr
.
match
(
UNORDERED_LIST_REG
);
if
(
!
matchResult
)
{
return
0
;
}
const
matchStr
=
matchResult
[
0
];
return
matchStr
.
length
;
};
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
renderer
=
(
rawStr
:
string
):
string
=>
{
const
matchResult
=
rawStr
.
match
(
UNORDERED_LIST_REG
);
const
matchResult
=
rawStr
.
match
(
UNORDERED_LIST_REG
);
if
(
!
matchResult
)
{
if
(
!
matchResult
)
{
return
rawStr
;
return
rawStr
;
}
}
const
parsedContent
=
marked
(
matchResult
[
1
],
inlineElementParserList
);
const
parsedContent
=
marked
(
matchResult
[
1
],
[],
inlineElementParserList
);
return
`<p><span class='ul-block'>•</span>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
return
`<p><span class='ul-block'>•</span>
${
parsedContent
}
</p>
${
matchResult
[
2
]}
`
;
};
};
export
default
{
export
default
{
name
:
"unordered list"
,
name
:
"unordered list"
,
regex
:
UNORDERED_LIST_REG
,
regex
:
UNORDERED_LIST_REG
,
match
,
renderer
,
renderer
,
};
};
web/src/labs/marked/parser/index.ts
View file @
eaf89aa2
...
@@ -16,17 +16,12 @@ import InlineCode from "./InlineCode";
...
@@ -16,17 +16,12 @@ import InlineCode from "./InlineCode";
export
{
CODE_BLOCK_REG
}
from
"./CodeBlock"
;
export
{
CODE_BLOCK_REG
}
from
"./CodeBlock"
;
export
{
TODO_LIST_REG
}
from
"./TodoList"
;
export
{
TODO_LIST_REG
}
from
"./TodoList"
;
export
{
DONE_LIST_REG
}
from
"./DoneList"
;
export
{
DONE_LIST_REG
}
from
"./DoneList"
;
export
{
ORDERED_LIST_REG
}
from
"./OrderedList"
;
export
{
UNORDERED_LIST_REG
}
from
"./UnorderedList"
;
export
{
PARAGRAPH_REG
}
from
"./Paragraph"
;
export
{
TAG_REG
}
from
"./Tag"
;
export
{
TAG_REG
}
from
"./Tag"
;
export
{
IMAGE_REG
}
from
"./Image"
;
export
{
IMAGE_REG
}
from
"./Image"
;
export
{
LINK_REG
}
from
"./Link"
;
export
{
LINK_REG
}
from
"./Link"
;
export
{
MARK_REG
}
from
"./Mark"
;
export
{
MARK_REG
}
from
"./Mark"
;
export
{
BOLD_REG
}
from
"./Bold"
;
export
{
EMPHASIS_REG
}
from
"./Emphasis"
;
// The order determines the order of execution.
// The order determines the order of execution.
export
const
blockElementParserList
=
[
CodeBlock
,
TodoList
,
DoneList
,
OrderedList
,
UnorderedList
,
Paragraph
];
export
const
blockElementParserList
=
[
CodeBlock
,
TodoList
,
DoneList
,
OrderedList
,
UnorderedList
,
Paragraph
];
export
const
inlineElementParserList
=
[
Image
,
Mark
,
Link
,
Bold
,
Emphasis
,
InlineCode
,
PlainLink
,
Tag
];
export
const
inlineElementParserList
=
[
Image
,
Mark
,
Bold
,
Emphasis
,
Link
,
InlineCode
,
PlainLink
,
Tag
];
export
const
parserList
=
[...
blockElementParserList
,
...
inlineElementParserList
];
export
const
parserList
=
[...
blockElementParserList
,
...
inlineElementParserList
];
web/src/less/memo-content.less
View file @
eaf89aa2
...
@@ -19,7 +19,7 @@
...
@@ -19,7 +19,7 @@
}
}
.img {
.img {
@apply float-left max-w-full
w-full
;
@apply float-left max-w-full;
}
}
.tag-span {
.tag-span {
...
@@ -52,7 +52,7 @@
...
@@ -52,7 +52,7 @@
}
}
code {
code {
@apply bg-gray-100 px-1 rounded text-sm leading-6 inline-block;
@apply bg-gray-100 px-1 rounded text-sm
font-mono
leading-6 inline-block;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment