11import { describe , it , expect } from "vitest" ;
22import { REGEX_CUSTOM } from "../src/index" ;
33
4- // Define a custom string method
5- String . prototype . normalize = function ( ) {
6- return this ?. replace ( / [ { } ] / g, "" )
7- . replace ( "." , " ." )
8- . replace ( "#" , " #" )
4+ /**
5+ *
6+ * normalize specific identifiers "{section#id.classname}" --> "section #id .classname"
7+ *
8+ */
9+ function normalizeIdentifiers ( input ?: string ) : string | undefined {
10+ return input
11+ ?. replace ( / [ { } ] / g, "" )
12+ . replace ( / \. / g, " ." )
13+ . replace ( / # / g, " #" )
914 . replace ( / \s + / g, " " )
1015 . trim ( ) ;
11- } ;
16+ }
1217
1318/**
14- * the matched title may contain additional props for container and title node
15- * in curly braces like: {section#foo} Title {span.bar}
19+ *
20+ * extract specific identifiers in curly braces for container and title nodes
21+ * ::: type {section#foo} title {span.bar}
1622 *
1723 */
18- function getCustomProps ( input : string ) {
24+ function extractSpecificIdentifiers ( input ?: string ) : {
25+ containerProps ?: string [ ] ;
26+ mainTitle ?: string ;
27+ titleProps ?: string [ ] ;
28+ } {
29+ if ( ! input ) return { } ;
30+
1931 const match = input . match ( REGEX_CUSTOM ) ;
2032
21- // eslint-disable- next-line
22- let [ input_ , containerFixture , mainTitle , titleFixture ] = match ?? [ undefined ] ;
33+ /* v8 ignore next */
34+ const [ , containerFixture , mainTitle , titleFixture ] = match ?? [ undefined ] ;
2335
24- containerFixture = containerFixture ?. normalize ( ) ;
36+ const nContainerFixture = normalizeIdentifiers ( containerFixture ) ;
37+ const nMainTitle = normalizeIdentifiers ( mainTitle ) ;
38+ const nTitleFixture = normalizeIdentifiers ( titleFixture ) ;
2539
2640 const containerProps =
27- containerFixture && containerFixture !== "" ? containerFixture ?. split ( " " ) : undefined ;
28-
29- titleFixture = titleFixture ?. normalize ( ) ;
30-
31- const titleProps = titleFixture && titleFixture !== "" ? titleFixture ?. split ( " " ) : undefined ;
32-
33- mainTitle = mainTitle ?. normalize ( ) ;
41+ nContainerFixture && nContainerFixture !== "" ? nContainerFixture . split ( " " ) : undefined ;
3442
35- mainTitle = mainTitle === "" ? undefined : mainTitle ;
43+ const titleProps =
44+ nTitleFixture && nTitleFixture !== "" ? nTitleFixture . split ( " " ) : undefined ;
3645
37- return { containerProps, mainTitle, titleProps } ;
46+ return { containerProps, mainTitle : nMainTitle || undefined , titleProps } ;
3847}
3948
4049describe ( "regex for custom props" , ( ) => {
4150 it ( "gets custom props 1" , ( ) => {
42- const input = "{ details#xxx .fff } Title My Spaces { summary #box }" ;
51+ const input = "{ details#xxx .fff.ggg } Title My Spaces { summary #box }" ;
4352
44- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
53+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
4554 {
4655 "containerProps": [
4756 "details",
4857 "#xxx",
4958 ".fff",
59+ ".ggg",
5060 ],
5161 "mainTitle": "Title My Spaces",
5262 "titleProps": [
@@ -60,7 +70,7 @@ describe("regex for custom props", () => {
6070 it ( "gets custom props 2" , ( ) => {
6171 const input = " Title My Spaces { summary #box }" ;
6272
63- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
73+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
6474 {
6575 "containerProps": undefined,
6676 "mainTitle": "Title My Spaces",
@@ -75,7 +85,7 @@ describe("regex for custom props", () => {
7585 it ( "gets custom props 3" , ( ) => {
7686 const input = "{ details#xxx .fff } Title My Spaces " ;
7787
78- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
88+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
7989 {
8090 "containerProps": [
8191 "details",
@@ -91,7 +101,7 @@ describe("regex for custom props", () => {
91101 it ( "gets custom props 4" , ( ) => {
92102 const input = "{ details#xxx .fff }" ;
93103
94- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
104+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
95105 {
96106 "containerProps": [
97107 "details",
@@ -107,7 +117,7 @@ describe("regex for custom props", () => {
107117 it ( "gets custom props 5" , ( ) => {
108118 const input = "{ details#xxx .fff } { summary #box }" ;
109119
110- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
120+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
111121 {
112122 "containerProps": [
113123 "details",
@@ -126,7 +136,7 @@ describe("regex for custom props", () => {
126136 it ( "gets custom props 6" , ( ) => {
127137 const input = " Title My Spaces " ;
128138
129- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
139+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
130140 {
131141 "containerProps": undefined,
132142 "mainTitle": "Title My Spaces",
@@ -138,7 +148,7 @@ describe("regex for custom props", () => {
138148 it ( "gets custom props 7" , ( ) => {
139149 const input = "{ } Title My Spaces {}" ;
140150
141- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
151+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
142152 {
143153 "containerProps": undefined,
144154 "mainTitle": "Title My Spaces",
@@ -150,7 +160,7 @@ describe("regex for custom props", () => {
150160 it ( "gets custom props 8" , ( ) => {
151161 const input = "{ } {}" ;
152162
153- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
163+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
154164 {
155165 "containerProps": undefined,
156166 "mainTitle": undefined,
@@ -162,7 +172,7 @@ describe("regex for custom props", () => {
162172 it ( "gets custom props 9" , ( ) => {
163173 const input = " {}" ;
164174
165- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
175+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
166176 {
167177 "containerProps": undefined,
168178 "mainTitle": undefined,
@@ -174,7 +184,7 @@ describe("regex for custom props", () => {
174184 it ( "gets custom props 10" , ( ) => {
175185 const input = " " ;
176186
177- expect ( getCustomProps ( input ) ) . toMatchInlineSnapshot ( `
187+ expect ( extractSpecificIdentifiers ( input ) ) . toMatchInlineSnapshot ( `
178188 {
179189 "containerProps": undefined,
180190 "mainTitle": undefined,
0 commit comments