Skip to content

Commit 8585eac

Browse files
authored
fix markdown content generation in codeql cli manual pages (#36014)
1 parent 2201c9b commit 8585eac

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/automated-pipelines/lib/update-markdown.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,27 @@ async function updateMarkdownFile(
8787
// update only the versions property of the file, assuming
8888
// the other properties have already been added and edited
8989
const { data, content } = matter(await readFile(file, 'utf-8'))
90-
if (isEqual(sourceData.versions, data.versions)) {
90+
91+
// Double check that the comment delimiter is only used once
92+
const matcher = new RegExp(commentDelimiter, 'g')
93+
const matches = content.match(matcher)
94+
if (matches && matches.length > 1) {
95+
throw new Error(`Error: ${file} has multiple comment delimiters`)
96+
}
97+
98+
// Only proceed if the content or versions have changed
99+
const [manuallyCreatedContent, automatedContent] = content.split(commentDelimiter)
100+
const isContentSame = automatedContent === sourceContent
101+
const isVersionsSame = isEqual(sourceData.versions, data.versions)
102+
if (isContentSame && isVersionsSame) {
91103
return
92104
}
105+
106+
// Create a new object so that we don't mutate the original data
93107
const newData = { ...data }
94-
// Keep all frontmatter currently in the Markdown file on disk
95-
// except replace the versions property with the new versions
108+
// Only modify the versions property when a file already existss
96109
newData.versions = sourceData.versions
97-
const targetContent = content.replace(commentDelimiter, sourceContent)
110+
const targetContent = manuallyCreatedContent + commentDelimiter + sourceContent
98111
await writeFile(file, matter.stringify(targetContent, newData))
99112
} else {
100113
await createDirectory(path.dirname(file))

src/codeql-cli/scripts/convert-markdown-for-docs.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@ export async function convertContentToDocs(content, frontmatterDefaults = {}) {
174174
if (node.type === 'link' && node.url.includes('aka.ms')) {
175175
akaMsLinkMatches.push(node)
176176
}
177+
178+
// There are example links in the format https://containers.GHEHOSTNAME
179+
// that we don't want our link checker to check so we need to make them
180+
// inline code instead of links. Ideally, this should be done in the
181+
// Java program that generates the rst files, but we can do it here for now.
182+
// See https://github.com/syntax-tree/mdast#inlinecode
183+
if (node.type === 'link' && node.url.startsWith('https://containers')) {
184+
// The nodes before and after contain double quotes that we want to remove
185+
const nodeBefore = ancestors[ancestors.length - 1].children[0]
186+
const nodeAfter = ancestors[ancestors.length - 1].children[2]
187+
if (nodeBefore.value.endsWith('"')) {
188+
nodeBefore.value = nodeBefore.value.slice(0, -1)
189+
}
190+
if (nodeAfter.value.startsWith('"')) {
191+
nodeAfter.value = nodeAfter.value.slice(1)
192+
}
193+
// Change the node to an inline code node
194+
node.type = 'inlineCode'
195+
node.value = node.url
196+
node.title = undefined
197+
node.url = undefined
198+
node.children = undefined
199+
}
177200
})
178201

179202
// Convert all aka.ms links to the docs.github.com relative path

src/codeql-cli/scripts/sync.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ import path from 'path'
99
import matter from 'gray-matter'
1010
import rimraf from 'rimraf'
1111

12-
import {
13-
updateContentDirectory,
14-
MARKDOWN_COMMENT,
15-
} from '../../automated-pipelines/lib/update-markdown.js'
12+
import { updateContentDirectory } from '../../automated-pipelines/lib/update-markdown.js'
1613
import { convertContentToDocs } from './convert-markdown-for-docs.js'
1714

1815
const { targetDirectory, sourceDirectory, frontmatterDefaults, markdownPrefix } = JSON.parse(
1916
await readFile(path.join('src/codeql-cli/lib/config.json'), 'utf-8')
2017
)
2118
const SOURCE_REPO = sourceDirectory.split('/')[0]
2219
const TEMP_DIRECTORY = path.join(SOURCE_REPO, 'tempCliDocs')
20+
const MARKDOWN_PREFIX = `${markdownPrefix}\n\n`
2321

2422
main()
2523

@@ -41,7 +39,7 @@ async function main() {
4139
await writeFile(file, matter.stringify(content, data))
4240
const targetFilename = path.join(targetDirectory, path.basename(file))
4341
const sourceData = { ...data, ...frontmatterDefaults }
44-
const finalSourceContent = MARKDOWN_COMMENT + `${markdownPrefix}\n\n` + content
42+
const finalSourceContent = MARKDOWN_PREFIX + content
4543
cliMarkdownContents[targetFilename] = { data: sourceData, content: finalSourceContent }
4644
}
4745
// Begin updating Markdown files in the content directory

0 commit comments

Comments
 (0)