-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ES|QL] gather inline function documentation from Elasticsearch #184689
Merged
drewdaemon
merged 19 commits into
elastic:main
from
drewdaemon:generate-inline-documentation
Jun 10, 2024
Merged
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f97eda3
docs generator script
drewdaemon 824e452
generate function documentation
drewdaemon 394259c
generate function docs script
drewdaemon d47b4d9
rename script
drewdaemon 2f4473a
update comment
drewdaemon a66683f
fix i18n
drewdaemon d058cd3
Merge branch 'main' of github.com:elastic/kibana into generate-inline…
drewdaemon 1cca237
fix translations
drewdaemon 52b49b9
deal with internal doc references
drewdaemon bbb7fc3
remove inline asciidoc cross-references
drewdaemon efebbdf
Merge branch 'main' of github.com:elastic/kibana into generate-inline…
drewdaemon 6fde98f
simplify markdown invocations
drewdaemon 2e26b02
remove spatial functions section
drewdaemon cab6a0b
restore readonly
drewdaemon 3194feb
Merge branch 'main' of github.com:elastic/kibana into generate-inline…
drewdaemon 79c6d66
add warning comments
drewdaemon 8668b6c
better comment
drewdaemon 383a6d3
turn off tag parsing (future-proofing)
drewdaemon 1427cda
Merge branch 'main' into generate-inline-documentation
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/kbn-text-based-editor/scripts/generate_esql_docs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as recast from 'recast'; | ||
const n = recast.types.namedTypes; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { functions } from '../src/esql_documentation_sections'; | ||
|
||
(function () { | ||
const pathToElasticsearch = process.argv[2]; | ||
const functionDocs = loadFunctionDocs(pathToElasticsearch); | ||
writeFunctionDocs(functionDocs); | ||
})(); | ||
|
||
function loadFunctionDocs(pathToElasticsearch: string) { | ||
// Define the directory path | ||
const dirPath = path.join(pathToElasticsearch, '/docs/reference/esql/functions/kibana/docs'); | ||
|
||
// Read the directory | ||
const files = fs.readdirSync(dirPath); | ||
|
||
// Initialize an empty map | ||
const functionMap = new Map<string, string>(); | ||
|
||
// Iterate over each file in the directory | ||
for (const file of files) { | ||
// Ensure we only process .md files | ||
if (path.extname(file) === '.md') { | ||
// Read the file content | ||
const content = fs.readFileSync(path.join(dirPath, file), 'utf-8'); | ||
|
||
// Get the function name from the file name by removing the .md extension | ||
const functionName = path.basename(file, '.md'); | ||
|
||
// Add the function name and content to the map | ||
functionMap.set(functionName, content); | ||
} | ||
} | ||
|
||
return functionMap; | ||
} | ||
|
||
function writeFunctionDocs(functionDocs: Map<string, string>) { | ||
const codeStrings = Array.from(functionDocs.entries()).map( | ||
([name, doc]) => ` | ||
const foo = { | ||
label: i18n.translate( | ||
'textBasedEditor.query.textBasedLanguagesEditor.documentationESQL.${name}', | ||
{ | ||
defaultMessage: '${name.toUpperCase()}', | ||
} | ||
), | ||
description: ( | ||
<Markdown | ||
readOnly | ||
markdownContent={i18n.translate( | ||
'textBasedEditor.query.textBasedLanguagesEditor.documentationESQL.${name}.markdown', | ||
{ | ||
defaultMessage: \`${doc.replaceAll('`', '\\`')}\`, | ||
description: | ||
'Text is in markdown. Do not translate function names, special characters, or field names like sum(bytes)', | ||
} | ||
)} | ||
/> | ||
), | ||
};` | ||
); | ||
|
||
const pathToDocsFile = path.join(__dirname, '../src/esql_documentation_sections.tsx'); | ||
|
||
const ast = recast.parse(fs.readFileSync(pathToDocsFile, 'utf-8')); | ||
|
||
const functionsList = findFunctionsList(ast); | ||
|
||
functionsList.elements = codeStrings.map( | ||
(codeString) => recast.parse(codeString).program.body[0].declarations[0].init | ||
); | ||
|
||
const newFileContents = recast.print(ast); | ||
|
||
fs.writeFileSync(pathToDocsFile, newFileContents.code); | ||
} | ||
|
||
/** | ||
* This function searches the AST for the functions list | ||
*/ | ||
function findFunctionsList(ast: any): recast.types.namedTypes.ArrayExpression { | ||
let foundArray: recast.types.namedTypes.ArrayExpression | null = null; | ||
|
||
const functionsArrayIdentifier = Object.keys({ functions })[0]; | ||
|
||
recast.visit(ast, { | ||
visitVariableDeclarator(astPath) { | ||
if ( | ||
n.Identifier.check(astPath.node.id) && | ||
astPath.node.id.name === functionsArrayIdentifier | ||
) { | ||
this.traverse(astPath); | ||
} | ||
return false; | ||
}, | ||
visitProperty(astPath) { | ||
if ( | ||
n.Identifier.check(astPath.node.key) && | ||
astPath.node.key.name === 'items' && | ||
n.ArrayExpression.check(astPath.node.value) | ||
) { | ||
foundArray = astPath.node.value; | ||
this.abort(); | ||
} | ||
return false; | ||
}, | ||
}); | ||
|
||
if (!foundArray) { | ||
throw new Error('Could not find the functions array in the AST'); | ||
} | ||
|
||
return foundArray; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vadimkibana more AST fun :)