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
b6f19ca0
Unverified
Commit
b6f19ca0
authored
Dec 21, 2022
by
boojack
Committed by
GitHub
Dec 21, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: upsert tag based content (#816)
* feat: upsert tag based content * chore: update
parent
68a77b6e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
2 deletions
+82
-2
README.md
README.md
+1
-1
MemoEditor.tsx
web/src/components/MemoEditor.tsx
+9
-1
index.ts
web/src/labs/marked/index.ts
+72
-0
No files found.
README.md
View file @
b6f19ca0
...
...
@@ -5,7 +5,7 @@
<p
align=
"center"
>
<a
href=
"https://github.com/usememos/memos/stargazers"
><img
alt=
"GitHub stars"
src=
"https://img.shields.io/github/stars/usememos/memos"
/></a>
<a
href=
"https://hub.docker.com/r/neosmemo/memos"
><img
alt=
"Docker pull"
src=
"https://img.shields.io/docker/pulls/neosmemo/memos.svg"
/></a>
<
img
alt=
"Go report"
src=
"https://goreportcard.com/badge/github.com/usememos/memos"
/
>
<
a
href=
"https://discord.gg/tfPJa4UmAv"
><img
alt=
"Discord"
src=
"https://img.shields.io/badge/discord-chat-5865f2?logo=discord&logoColor=f5f5f5"
/></a
>
</p>
<p
align=
"center"
>
...
...
web/src/components/MemoEditor.tsx
View file @
b6f19ca0
import
{
isNumber
,
last
,
toLower
}
from
"lodash"
;
import
{
isNumber
,
last
,
toLower
,
uniq
}
from
"lodash"
;
import
React
,
{
useCallback
,
useEffect
,
useMemo
,
useRef
,
useState
}
from
"react"
;
import
{
useTranslation
}
from
"react-i18next"
;
import
{
getMatchedNodes
}
from
"../labs/marked"
;
import
{
deleteMemoResource
,
upsertMemoResource
}
from
"../helpers/api"
;
import
{
TAB_SPACE_WIDTH
,
UNKNOWN_ID
,
VISIBILITY_SELECTOR_ITEMS
}
from
"../helpers/consts"
;
import
{
useEditorStore
,
useLocationStore
,
useMemoStore
,
useResourceStore
,
useTagStore
,
useUserStore
}
from
"../store/module"
;
...
...
@@ -326,6 +327,13 @@ const MemoEditor = () => {
toastHelper
.
error
(
error
.
response
.
data
.
message
);
}
// Upsert tag based with content.
const
matchedNodes
=
getMatchedNodes
(
content
);
const
tagNameList
=
uniq
(
matchedNodes
.
filter
((
node
)
=>
node
.
parserName
===
"tag"
).
map
((
node
)
=>
node
.
matchedContent
.
slice
(
1
)));
for
(
const
tagName
of
tagNameList
)
{
await
tagStore
.
upsertTag
(
tagName
);
}
setState
((
state
)
=>
{
return
{
...
state
,
...
...
web/src/labs/marked/index.ts
View file @
b6f19ca0
...
...
@@ -53,3 +53,75 @@ export const marked = (markdownStr: string, blockParsers = blockElementParserLis
return
markdownStr
;
};
interface
MatchedNode
{
parserName
:
string
;
matchedContent
:
string
;
}
export
const
getMatchedNodes
=
(
markdownStr
:
string
):
MatchedNode
[]
=>
{
const
matchedNodeList
:
MatchedNode
[]
=
[];
const
walkthough
=
(
markdownStr
:
string
,
blockParsers
=
blockElementParserList
,
inlineParsers
=
inlineElementParserList
):
string
=>
{
for
(
const
parser
of
blockParsers
)
{
const
matchResult
=
parser
.
matcher
(
markdownStr
);
if
(
!
matchResult
)
{
continue
;
}
const
matchedStr
=
matchResult
[
0
];
const
retainContent
=
markdownStr
.
slice
(
matchedStr
.
length
);
matchedNodeList
.
push
({
parserName
:
parser
.
name
,
matchedContent
:
matchedStr
,
});
if
(
parser
.
name
===
"br"
)
{
return
walkthough
(
retainContent
,
blockParsers
,
inlineParsers
);
}
else
{
if
(
retainContent
.
startsWith
(
"
\n
"
))
{
return
walkthough
(
retainContent
.
slice
(
1
),
blockParsers
,
inlineParsers
);
}
}
}
let
matchedInlineParser
=
undefined
;
let
matchedIndex
=
-
1
;
for
(
const
parser
of
inlineParsers
)
{
const
matchResult
=
parser
.
matcher
(
markdownStr
);
if
(
!
matchResult
)
{
continue
;
}
if
(
parser
.
name
===
"plain text"
&&
matchedInlineParser
!==
undefined
)
{
continue
;
}
const
startIndex
=
matchResult
.
index
as
number
;
if
(
matchedInlineParser
===
undefined
||
matchedIndex
>
startIndex
)
{
matchedInlineParser
=
parser
;
matchedIndex
=
startIndex
;
}
}
if
(
matchedInlineParser
)
{
const
matchResult
=
matchedInlineParser
.
matcher
(
markdownStr
);
if
(
matchResult
)
{
const
matchedStr
=
matchResult
[
0
];
const
matchedLength
=
matchedStr
.
length
;
const
suffixStr
=
markdownStr
.
slice
(
matchedIndex
+
matchedLength
);
matchedNodeList
.
push
({
parserName
:
matchedInlineParser
.
name
,
matchedContent
:
matchedStr
,
});
return
walkthough
(
suffixStr
,
[],
inlineParsers
);
}
}
return
markdownStr
;
};
walkthough
(
markdownStr
);
return
matchedNodeList
;
};
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