MCPcopy Create free account
hub / github.com/webdriverio/webdriverio / normalizeDoc

Function normalizeDoc

scripts/docs-generation/3rdPartyDocs.ts:120–181  ·  view source on GitHub ↗

* Removes header from README.md * @param {string} readme readme content * @param {string} githubUrl repo url * @param {string} branch repo branch * @param {string} preface docusaurus header * @param {string} repoInfo repoInfo * @return {string} readme conte

(readme: string, githubUrl: string, branch: string, preface: string[], repoInfo: string[], packageName: string)

Source from the content-addressed store, hash-verified

118 * @return {string} readme content without header
119 */
120function normalizeDoc(readme: string, githubUrl: string, branch: string, preface: string[], repoInfo: string[], packageName: string) {
121 /**
122 * remove badges
123 */
124 let readmeArr = readme.split('\n').filter(row => !readmeBadges.some(b => row.includes(b)))
125
126 /**
127 * get index of header to remove further
128 */
129 let sliceIdx = 0
130 for (let i = 0; i < readmeHeaderLines && sliceIdx === 0; i++) {
131 if (typeof readmeArr[i] === 'string' && readmeHeaders.some(x => readmeArr[i].startsWith(x))) {
132 sliceIdx = i + 1
133 }
134 }
135 readmeArr = readmeArr.slice(sliceIdx)
136
137 /**
138 * prepend additional # to header rows
139 * and add path to links
140 */
141 readmeArr.forEach((row, idx) => {
142 /**
143 * prepend # to header rows
144 */
145 if (row.match(/^(# )\1* /)) {
146 readmeArr[idx] = `#${row}`
147 }
148
149 /**
150 * transform partial to full links
151 * match links like [foo](bar). `stringInParentheses` would be `bar`
152 * do not match [foo](http://bar), [foo](#bar)
153 */
154 const mdLinks = row.match(/\[([^\]]+)\]\(([^)"]+)(?: "([^"]+)")?\)/g) || []
155 for (const mdLink of mdLinks) {
156 const urlMatcher = mdLink.match(/\[([^[]+)\]\((.*)\)/)
157
158 if (!urlMatcher) {
159 continue
160 }
161
162 const stringInParentheses = urlMatcher[2]
163 const url = ( stringInParentheses.startsWith('http') || stringInParentheses.startsWith('#') )
164 ? stringInParentheses
165 : `${githubUrl}/blob/${branch}/${stringInParentheses}`
166 readmeArr[idx] = readmeArr[idx].replace(`](${stringInParentheses})`, `](${url})`)
167 }
168 })
169
170 /**
171 * apply custom fixes
172 */
173 if (docsFixes[packageName]) {
174 console.log(`Applying custom fixes for ${packageName}`)
175 readmeArr = [docsFixes[packageName](readmeArr.join('\n'))]
176 }
177

Callers 1

generate3rdPartyDocsFunction · 0.85

Calls 2

sanitizeHtmlForMdxFunction · 0.85
logMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…