1
- import { QuickStart , QuickStartTask } from "@patternfly/quickstarts" ;
1
+ /* eslint-disable */
2
2
3
- export type GuidesQuickStart = QuickStart & {
4
- metadata ?: {
5
- annotations ?: {
6
- draft ?: boolean ,
7
- order ?: number
8
- }
9
- }
10
- spec : {
11
- tasks : undefined | QuickStartTask [ ] | string [ ]
12
- }
13
- }
3
+ import { QuickStart , QuickStartTask } from '@patternfly/quickstarts' ;
4
+ import { ProcQuickStartParser } from "@app/patternfly-procedure-parser" ;
14
5
15
-
16
- export const ProcQuickStartParser = (
17
- quickStart : GuidesQuickStart ,
6
+ export const ProcQuickStartParserWithImageSupport = (
7
+ quickStart : QuickStart & {
8
+ spec : {
9
+ tasks : undefined | QuickStartTask [ ] | string [ ] ;
10
+ } ;
11
+ } ,
18
12
basePath : string ,
19
- environmentVariables ?: { [ name : string ] : string }
13
+ environmentVariables ?: { [ name : string ] : string } ,
20
14
) => {
21
- const replaceEnvironmentVariables = ( s : string | undefined ) =>
22
- s ?. replace ( / \$ { ( \w + ) } / , ( substring , name ) => {
23
- return environmentVariables ? [ name ]
24
- ? environmentVariables [ name ]
25
- : substring : substring ;
26
- } ) ;
27
15
28
- quickStart . spec . tasks = quickStart . spec . tasks ?. map ( ( task : QuickStartTask | string , index ) => {
29
- let proc : string ;
30
- let answer : QuickStartTask ;
31
- if ( typeof task === "string" ) {
32
- proc = task ;
33
- answer = { } ;
34
- } else {
35
- proc = task [ "proc" ]
36
- answer = task ;
37
- delete task [ "proc" ] ;
38
- }
16
+ // Use the upstream parser
17
+ const resource = ProcQuickStartParser ( quickStart , environmentVariables ) ;
39
18
40
- let procedure , verification , title , summaryFailed , success , reviewFailed : string | undefined ;
41
- let description = "" ;
42
- if ( proc ) {
43
- const parser = new DOMParser ( ) ;
44
- proc = proc . replace ( "<img src=\"\./images" , "<img src=\"" + basePath + "/images" ) ;
45
- const taskDOM = parser . parseFromString ( proc , 'text/html' ) ;
19
+ // add image path fixing
20
+ function fixImagePath ( str : string ) : string ;
21
+ function fixImagePath ( str : undefined | string ) : undefined | string ;
22
+ function fixImagePath ( str : string | undefined ) : string | undefined {
23
+ return str === undefined ? undefined : str . replace ( "<img src=\"\./images" , "<img src=\"" + basePath + "/images" ) ;
24
+ }
46
25
47
- // remove the screencapture images
48
- taskDOM . querySelectorAll ( ".imageblock.screencapture" ) . forEach ( node => {
49
- node . parentElement ?. removeChild ( node ) ;
50
- } ) ;
26
+ resource . spec . tasks = resource . spec . tasks ?. map ( task => {
51
27
52
- title = taskDOM . querySelector ( "h1:first-child,h2:first-child,h3:first-child,h4:first-child,h5:first-child" ) ?. innerHTML . trim ( ) ;
53
- let sectionBody = taskDOM . querySelector ( ".sectionbody" ) ;
54
- if ( ! sectionBody ?. hasChildNodes ( ) ) {
55
- // possibly in other templates, where we want to look for article
56
- sectionBody = taskDOM . querySelector ( "article" ) ;
57
- }
58
- if ( sectionBody ) {
59
- for ( let i = 0 ; i < sectionBody . children . length || 0 ; i ++ ) {
60
- const child = sectionBody . children . item ( i ) ;
61
- // find the title
62
- const title = child ?. querySelector ( ".heading,.title" ) ;
63
- if ( title ) {
64
- switch ( title ?. textContent ?. trim ( ) ) {
65
- case "Procedure" :
66
- procedure = child ?. querySelector ( ":not(.heading):not(.title)" ) ?. outerHTML . trim ( ) ;
67
- break ;
68
- case "Verification" :
69
- verification = child ?. querySelector ( ":not(.heading):not(.title)" ) ?. outerHTML . trim ( ) ;
70
- break ;
71
- }
72
- } else if ( ! procedure ) {
73
- // Otherwise if it comes before a procedure it's part of the description
74
- description += child ?. innerHTML . trim ( ) ;
75
- }
76
- }
77
- }
78
- success = taskDOM . querySelector ( ".qs-summary.success" ) ?. innerHTML . trim ( ) ;
79
- reviewFailed = taskDOM . querySelector ( ".qs-review.failed" ) ?. innerHTML . trim ( ) ;
80
- summaryFailed = taskDOM . querySelector ( ".qs-summary.failed" ) ?. innerHTML . trim ( ) ;
28
+ task . description = fixImagePath ( task . description ) ;
29
+ if ( task . summary !== undefined ) {
30
+ task . summary . success = fixImagePath ( task . summary . success ) ;
31
+ task . summary . failed = fixImagePath ( task . summary . failed ) ;
81
32
}
82
-
83
-
84
- answer . title = replaceEnvironmentVariables ( answer . title || title )
85
- answer . description = replaceEnvironmentVariables ( answer . description || `${ description } ${ procedure } ` ) ;
86
- answer . review = answer . review || { } ;
87
- answer . review . instructions = replaceEnvironmentVariables ( answer . review ?. instructions || verification || "Have you completed these steps?" )
88
- answer . review . failedTaskHelp = replaceEnvironmentVariables ( answer . review . failedTaskHelp || reviewFailed || "This task isn’t verified yet. Try the task again." ) ;
89
- answer . summary = answer . summary || { } ;
90
- answer . summary . success = replaceEnvironmentVariables ( answer . summary . success ||
91
- success
92
- || "You have completed this task!" ) ;
93
- answer . summary . failed = replaceEnvironmentVariables ( answer . summary . failed || summaryFailed
94
- || "Try the steps again." ) ;
95
- return answer ;
33
+ if ( task . review !== undefined ) {
34
+ task . review . failedTaskHelp = fixImagePath ( task . review . failedTaskHelp ) ;
35
+ task . review . instructions = fixImagePath ( task . review . instructions ) ;
36
+ }
37
+ task . title = fixImagePath ( task . title ) ;
38
+ return task ;
96
39
} ) ;
97
- return quickStart ;
98
- } ;
40
+ resource . spec . description = fixImagePath ( resource . spec . description )
41
+ resource . spec . introduction = fixImagePath ( resource . spec . introduction )
42
+ resource . spec . conclusion = fixImagePath ( resource . spec . conclusion ) ;
43
+ return resource ;
44
+ } ;
0 commit comments