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

[minor]: Run prettier on all JS code blocks #187

Merged
merged 4 commits into from
Apr 23, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"isomorphic-dompurify": "^2.7.0",
"json-loader": "^0.5.7",
"marked": "^12.0.1",
"prettier": "^2.7.1",
"process": "^0.11.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand All @@ -62,7 +63,6 @@
"jest": "^29.6.2",
"langchain": "^0.0.179",
"langsmith": "^0.0.47",
"prettier": "^2.7.1",
"supabase": "^1.148.6",
"ts-jest": "^29.1.1",
"typedoc": "^0.24.4",
Expand Down
31 changes: 30 additions & 1 deletion src/components/InstructionsWithCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import CodeBlock from "@theme/CodeBlock";
import { marked } from "marked";
import DOMPurify from "isomorphic-dompurify";
import prettier from "prettier";
import parserTypeScript from "prettier/parser-typescript";

export function LangChainPyBlock(content) {
return {
Expand Down Expand Up @@ -61,6 +63,33 @@
};
}

/**
* @param {string} code
* @param {"typescript" | "python"} language
* @returns {string} The formatted code
*/
function formatCode(code, language) {
const languageLower = language.toLowerCase();
if (languageLower === "python") {
// no-op - Do not format Python code at this time
return code;
}

try {
const formattedCode = prettier.format(code, {
parser: languageLower,
plugins: [parserTypeScript],
});

return formattedCode;
} catch (_) {
// no-op
}

// If formatting fails, return as is
return code;
}

export function CodeTabs({ tabs, groupId }) {
return (
<Tabs groupId={groupId}>
Expand All @@ -70,7 +99,7 @@
<TabItem key={key} value={tab.value} label={tab.label}>
{tab.caption && (
<div
dangerouslySetInnerHTML={{

Check warning on line 102 in src/components/InstructionsWithCode.js

View workflow job for this annotation

GitHub Actions / Check linting

Dangerous property 'dangerouslySetInnerHTML' found
__html: DOMPurify.sanitize(marked.parse(tab.caption)),
}}
/>
Expand All @@ -79,7 +108,7 @@
className={tab.value}
language={tab.language ?? tab.value}
>
{tab.content}
{formatCode(tab.content, tab.language ?? tab.value)}
</CodeBlock>
</TabItem>
);
Expand Down
Loading