Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
sfm-js
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dakkar
sfm-js
Commits
d95206fe
Commit
d95206fe
authored
3 years ago
by
marihachi
Browse files
Options
Downloads
Patches
Plain Diff
#32
parent
4fe75b13
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/util.ts
+20
-42
20 additions, 42 deletions
src/util.ts
with
20 additions
and
42 deletions
src/util.ts
+
20
−
42
View file @
d95206fe
import
{
MfmNode
,
MfmText
}
from
'
./node
'
;
import
{
MfmNode
}
from
'
./node
'
;
export
function
createNode
(
type
:
string
,
props
?:
Record
<
string
,
any
>
,
children
?:
MfmNode
[]):
MfmNode
{
const
node
:
any
=
{
type
};
...
...
@@ -11,54 +11,32 @@ export function createNode(type: string, props?: Record<string, any>, children?:
return
node
;
}
/**
* @param predicate specifies whether to group the previous item and the current item
* @returns grouped items
*/
export
function
groupContinuous
<
T
>
(
arr
:
T
[],
predicate
:
(
prev
:
T
,
current
:
T
)
=>
boolean
):
T
[][]
{
const
dest
:
any
[][]
=
[];
export
function
mergeText
(
nodes
:
(
MfmNode
|
string
)[]):
MfmNode
[]
{
const
dest
:
MfmNode
[]
=
[];
const
storedChars
:
string
[]
=
[];
for
(
let
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
if
(
i
!=
0
&&
predicate
(
arr
[
i
-
1
],
arr
[
i
]))
{
dest
[
dest
.
length
-
1
].
push
(
arr
[
i
]);
}
else
{
dest
.
push
([
arr
[
i
]]);
/**
* Generate a text node from the stored chars, And push it.
*/
function
generateText
()
{
if
(
storedChars
.
length
>
0
)
{
const
textNode
=
createNode
(
'
text
'
,
{
text
:
storedChars
.
join
(
''
)
});
dest
.
push
(
textNode
);
storedChars
.
length
=
0
;
}
}
return
dest
;
}
export
function
mergeGroupedTrees
<
T
>
(
groupedTrees
:
T
[][]):
T
[]
{
return
groupedTrees
.
reduce
((
acc
,
val
)
=>
acc
.
concat
(
val
),
([]
as
T
[]));
}
export
function
mergeText
(
trees
:
(
MfmNode
|
string
)[]):
MfmNode
[]
{
// group trees
const
groupes
=
groupContinuous
(
trees
,
(
prev
,
current
)
=>
{
if
(
typeof
prev
==
'
string
'
||
typeof
current
==
'
string
'
)
{
return
(
typeof
prev
==
'
string
'
&&
typeof
current
==
'
string
'
);
for
(
const
node
of
nodes
)
{
if
(
typeof
node
==
'
string
'
)
{
// Store the char.
storedChars
.
push
(
node
);
}
else
{
return
(
prev
.
type
==
current
.
type
);
generateText
();
dest
.
push
(
node
);
}
});
// concatinate text
const
concatGroupes
=
groupes
.
map
((
group
)
=>
{
if
(
typeof
group
[
0
]
==
'
string
'
)
{
return
[
createNode
(
'
text
'
,
{
text
:
(
group
as
string
[]).
join
(
''
)
})
];
}
return
(
group
as
MfmNode
[]);
});
// merge groups
const
dest
=
mergeGroupedTrees
(
concatGroupes
);
}
generateText
();
return
dest
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment