MCPcopy Index your code
hub / github.com/github/docs / addToChildren

Function addToChildren

script/move-content.js:350–393  ·  view source on GitHub ↗
(newPath, positions, opts)

Source from the content-addressed store, hash-verified

348}
349
350function addToChildren(newPath, positions, opts) {
351 const { verbose } = opts
352 const parentFilePath = path.join(path.dirname(newPath), 'index.md')
353 const fileContent = fs.readFileSync(parentFilePath, 'utf-8')
354 const { content, data } = readFrontmatter(fileContent)
355 const newName = getBasename(newPath)
356
357 const { childrenPosition, childGroupPositions } = positions
358 if (childrenPosition > -1) {
359 const children = data[CHILDREN_KEY] || []
360 let prefix = ''
361 if (children.every((entry) => entry.startsWith('/'))) {
362 prefix += '/'
363 }
364 if (childrenPosition > -1 && childrenPosition < children.length) {
365 children.splice(childrenPosition, 0, prefix + newName)
366 } else {
367 children.push(prefix + newName)
368 }
369 data[CHILDREN_KEY] = children
370 }
371
372 if (CHILDGROUPS_KEY in data) {
373 for (const [groupIndex, childrenPosition] of childGroupPositions) {
374 if (groupIndex < data[CHILDGROUPS_KEY].length) {
375 const group = data[CHILDGROUPS_KEY][groupIndex]
376 if (childrenPosition < group.children.length) {
377 group.children.splice(childrenPosition, 0, newName)
378 } else {
379 group.children.push(newName)
380 }
381 }
382 }
383 }
384
385 fs.writeFileSync(
386 parentFilePath,
387 readFrontmatter.stringify(content, data, { lineWidth: 10000 }),
388 'utf-8'
389 )
390 if (verbose) {
391 console.log(`Added 'children' (${newName}) key in ${parentFilePath}`)
392 }
393}
394
395function moveFiles(files, opts) {
396 const { verbose, git: useGit } = opts

Callers 3

mainFunction · 0.85
editFilesFunction · 0.85
undoFilesFunction · 0.85

Calls 2

readFrontmatterFunction · 0.85
getBasenameFunction · 0.85

Tested by

no test coverage detected