Skip to content
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

docs[patch]: Support hidden docs cells and TS validation in notebooks #6259

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/core_docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ src/supabase.d.ts
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/scripts/tmp

# ESLint
.eslintcache
Expand All @@ -33,8 +34,6 @@ yarn-error.log*

/.quarto/
# AUTO_GENERATED_DOCS
docs/tutorials/test.md
docs/tutorials/test.mdx
docs/tutorials/rag.md
docs/tutorials/rag.mdx
docs/tutorials/query_analysis.md
Expand Down
8 changes: 8 additions & 0 deletions docs/core_docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ Some common defaults for linting/formatting have been set for you. If you integr
```
$ yarn ci
```

### Validating Notebooks

You can validate that notebooks build and compile TypeScript using the following command:

```bash
$ yarn validate <PATH_TO_FILE>
```
15 changes: 15 additions & 0 deletions docs/core_docs/docs/how_to/streaming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// @ls-docs-hide-cell\n",
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"\n",
"const model = new ChatOpenAI({\n",
" model: \"gpt-4o\",\n",
" temperature: 0,\n",
"});"
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand Down
5 changes: 4 additions & 1 deletion docs/core_docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"gen:supabase": "npx supabase gen types typescript --project-id 'xsqpnijvmbodcxyapnyq' --schema public > ./src/supabase.d.ts",
"broken-links": "node ./scripts/check-broken-links.js",
"check:broken-links": "yarn quarto && yarn broken-links",
"check:broken-links:ci": "yarn quarto:vercel && yarn broken-links"
"check:broken-links:ci": "yarn quarto:vercel && yarn broken-links",
"validate": "tsx --experimental-wasm-modules -r dotenv/config ./scripts/validate_notebook.ts"
},
"dependencies": {
"@docusaurus/core": "2.4.3",
Expand Down Expand Up @@ -70,8 +71,10 @@
"rimraf": "^5.0.1",
"supabase": "^1.148.6",
"swc-loader": "^0.2.3",
"tsx": "^3.12.3",
"typedoc": "^0.24.4",
"typedoc-plugin-markdown": "next",
"typescript": "~5.1.6",
"yaml-loader": "^0.8.0"
},
"packageManager": "[email protected]",
Expand Down
11 changes: 11 additions & 0 deletions docs/core_docs/scripts/quarto-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const fs = require("node:fs");
const { glob } = require("glob");
const { execSync } = require("node:child_process");

const IGNORED_CELL_REGEX = /```\w*?\n\/\/ ?@ls-docs-hide-cell\n[\s\S]*?```/g;

async function main() {
const allIpynb = await glob("./docs/**/*.ipynb");

Expand All @@ -15,6 +17,15 @@ async function main() {
gitignore += "# AUTO_GENERATED_DOCS\n";
gitignore += allRenames.join("\n");
fs.writeFileSync(pathToRootGitignore, gitignore);
for (const renamedFilepath of allRenames) {
if (fs.existsSync(renamedFilepath)) {
let content = fs.readFileSync(renamedFilepath).toString();
if (content.match(IGNORED_CELL_REGEX)) {
content = content.replace(IGNORED_CELL_REGEX, "");
fs.writeFileSync(renamedFilepath, content);
}
}
}

try {
/**
Expand Down
85 changes: 85 additions & 0 deletions docs/core_docs/scripts/validate_notebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as fs from "node:fs/promises";
import * as ts from "typescript";
import { v4 as uuidv4 } from "uuid";

export async function extract(filepath: string) {
const cells = JSON.parse((await fs.readFile(filepath)).toString()).cells;
const code = cells
.map((cell: Record<string, any>) => {
if (cell.cell_type === "code") {
return cell.source.join("");
}
return "";
})
.join("\n");
return code;
}

let [pathname, ...args] = process.argv.slice(2);

if (!pathname) {
throw new Error("No pathname provided.");
}

const run = async () => {
if (pathname.startsWith("docs/core_docs/")) {
pathname = "./" + pathname.slice("docs/core_docs/".length);
}
if (!pathname.endsWith(".ipynb")) {
throw new Error("Only .ipynb files are supported.");
}
const filename = pathname
.split("/")
[pathname.split("/").length - 1].replace(".ipynb", ".mts");
const tempFilepath = `./tmp/${filename}`;
try {
const typescriptSource = await extract(pathname);
try {
await fs.access("./tmp", fs.constants.F_OK);
} catch (err) {
await fs.mkdir("./tmp");
}
await fs.writeFile(tempFilepath, typescriptSource);
const program = ts.createProgram([tempFilepath], {
module: ts.ModuleKind.NodeNext,
moduleResolution: ts.ModuleResolutionKind.NodeNext,
target: ts.ScriptTarget.ES2021,
alwaysStrict: true,
skipLibCheck: true,
});
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length === 0) {
console.log("No type errors found.");
} else {
diagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
const { line, character } =
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
const message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
"\n"
);
console.log(
`${diagnostic.file.fileName} (${line + 1},${
character + 1
}): ${message}`
);
} else {
console.log(
ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
);
}
});
}
} catch (e) {
console.log(e);
} finally {
try {
await fs.rm(tempFilepath);
} catch (e) {
// Do nothing
}
}
};

run();
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22775,8 +22775,10 @@ __metadata:
rimraf: ^5.0.1
supabase: ^1.148.6
swc-loader: ^0.2.3
tsx: ^3.12.3
typedoc: ^0.24.4
typedoc-plugin-markdown: next
typescript: ~5.1.6
uuid: ^10.0.0
webpack: ^5.75.0
yaml-loader: ^0.8.0
Expand Down
Loading