|
| 1 | +/* eslint-disable */ |
| 2 | +// Brought in from dev to publish this with QS module |
| 3 | +// Dev now imports from here |
| 4 | + |
| 5 | +import { QuickStart, QuickStartTask } from './quick-start-types'; |
| 6 | + |
| 7 | +export const ProcQuickStartParser = ( |
| 8 | + quickStart: QuickStart & { |
| 9 | + spec: { |
| 10 | + tasks: undefined | QuickStartTask[] | string[]; |
| 11 | + }; |
| 12 | + }, |
| 13 | + environmentVariables?: { [name: string]: string }, |
| 14 | +) => { |
| 15 | + const replaceEnvironmentVariables = (s: string | undefined) => |
| 16 | + s?.replace(/\${(\w+)}/, (substring, name) => { |
| 17 | + return environmentVariables ? ([name] ? environmentVariables[name] : substring) : substring; |
| 18 | + }); |
| 19 | + |
| 20 | + quickStart.spec.tasks = quickStart.spec.tasks?.map((task: QuickStartTask | string, index) => { |
| 21 | + let proc: string; |
| 22 | + let answer: QuickStartTask; |
| 23 | + if (typeof task === 'string') { |
| 24 | + proc = task; |
| 25 | + answer = {}; |
| 26 | + } else { |
| 27 | + // @ts-ignore |
| 28 | + proc = task.proc; |
| 29 | + answer = task; |
| 30 | + // @ts-ignore |
| 31 | + delete task.proc; |
| 32 | + } |
| 33 | + |
| 34 | + let description = '', |
| 35 | + procedure, |
| 36 | + verification, |
| 37 | + title, |
| 38 | + summaryFailed, |
| 39 | + success, |
| 40 | + reviewFailed: string | undefined, |
| 41 | + prerequisites; |
| 42 | + if (proc) { |
| 43 | + const taskDOM = document.createElement('div'); |
| 44 | + taskDOM.innerHTML = proc; |
| 45 | + |
| 46 | + // remove the screencapture images |
| 47 | + taskDOM.querySelectorAll('.imageblock.screencapture').forEach((node) => { |
| 48 | + node.parentElement?.removeChild(node); |
| 49 | + }); |
| 50 | + |
| 51 | + title = taskDOM |
| 52 | + .querySelector('h1:first-child,h2:first-child,h3:first-child,h4:first-child,h5:first-child') |
| 53 | + ?.innerHTML.trim(); |
| 54 | + let sectionBody = taskDOM.querySelector('.sectionbody'); |
| 55 | + if (!sectionBody?.hasChildNodes()) { |
| 56 | + // possibly in other templates, where we want to look for article |
| 57 | + sectionBody = taskDOM.querySelector('article'); |
| 58 | + } |
| 59 | + if (sectionBody) { |
| 60 | + for (let i = 0; i < sectionBody.children.length || 0; i++) { |
| 61 | + /** |
| 62 | + child typically looks like: |
| 63 | +
|
| 64 | + <div class="paragraph|olist|ulist|admonitionblock"> |
| 65 | + <div class="title">Procedure|Prerequisites|Verification|Note|Warning</div> |
| 66 | + <ol|ul class="arabic"> |
| 67 | + <li> |
| 68 | + <li>... |
| 69 | + </ol|ul> |
| 70 | + </div> |
| 71 | +
|
| 72 | + And the below code extracts the <ol> or <ul> |
| 73 | + Except for when there is no <div class="title|heading"/>, then the description is extracted |
| 74 | + in the else if below |
| 75 | + */ |
| 76 | + const child = sectionBody.children.item(i); |
| 77 | + // find the title |
| 78 | + const sectionTitle = child?.querySelector('.heading,.title'); |
| 79 | + // should this section be assigned to a specific section |
| 80 | + const sectionTitleText = sectionTitle?.textContent?.trim(); |
| 81 | + const isKnownSection = ['Procedure', 'Verification', 'Prerequisites'].includes( |
| 82 | + sectionTitle?.textContent?.trim(), |
| 83 | + ); |
| 84 | + if (isKnownSection) { |
| 85 | + switch (sectionTitleText) { |
| 86 | + case 'Procedure': |
| 87 | + procedure = child?.querySelector(':not(.heading):not(.title)')?.outerHTML.trim(); |
| 88 | + break; |
| 89 | + case 'Verification': |
| 90 | + verification = child?.querySelector(':not(.heading):not(.title)')?.outerHTML.trim(); |
| 91 | + break; |
| 92 | + case 'Prerequisites': |
| 93 | + prerequisites = child |
| 94 | + ?.querySelector(':not(.heading):not(.title)') |
| 95 | + ?.outerHTML.trim(); |
| 96 | + break; |
| 97 | + } |
| 98 | + } else if (!procedure) { |
| 99 | + // Otherwise if it comes before a procedure it's part of the description |
| 100 | + description = description + child?.outerHTML.trim(); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + success = taskDOM.querySelector('.qs-summary.success')?.innerHTML.trim(); |
| 105 | + reviewFailed = taskDOM.querySelector('.qs-review.failed')?.innerHTML.trim(); |
| 106 | + summaryFailed = taskDOM.querySelector('.qs-summary.failed')?.innerHTML.trim(); |
| 107 | + } |
| 108 | + |
| 109 | + answer.title = replaceEnvironmentVariables(answer.title || title); |
| 110 | + answer.description = replaceEnvironmentVariables( |
| 111 | + answer.description || `${description} ${prerequisites || ''} ${procedure}`, |
| 112 | + ); |
| 113 | + answer.review = answer.review || {}; |
| 114 | + answer.review.instructions = replaceEnvironmentVariables( |
| 115 | + answer.review?.instructions || verification || 'Have you completed these steps?', |
| 116 | + ); |
| 117 | + answer.review.failedTaskHelp = replaceEnvironmentVariables( |
| 118 | + answer.review.failedTaskHelp || |
| 119 | + reviewFailed || |
| 120 | + 'This task isn’t verified yet. Try the task again.', |
| 121 | + ); |
| 122 | + answer.summary = answer.summary || {}; |
| 123 | + answer.summary.success = replaceEnvironmentVariables( |
| 124 | + answer.summary.success || success || 'You have completed this task!', |
| 125 | + ); |
| 126 | + answer.summary.failed = replaceEnvironmentVariables( |
| 127 | + answer.summary.failed || summaryFailed || 'Try the steps again.', |
| 128 | + ); |
| 129 | + return answer; |
| 130 | + }); |
| 131 | + return quickStart; |
| 132 | +}; |
0 commit comments