Skip to content

added yaml editor #74

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

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions docs/configure-coderabbit.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ sidebar_position: 3
import SchemaViewer from "@site/src/components/SchemaViewer";
```

```mdx-code-block
import YamlEditor from "/src/components/YamlEditor/YamlEditor";
```

CodeRabbit offers various configuration options to tailor the reviews to your
requirements. Configuration can be made using one of the below options, in order
of precedence:
Expand Down Expand Up @@ -56,6 +60,12 @@ chat:
auto_reply: true
```

Write your configuration file in the below editor to validate:

```mdx-code-block
<YamlEditor />
```

The configuration file can be used to set the following options:

```mdx-code-block
Expand Down
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"postcss": "^8.4.32",
"prism-react-renderer": "^2.3.0",
"react": "^18.0.0",
"react-ace": "^12.0.0",
"react-dom": "^18.0.0",
"tailwindcss": "^3.4.0"
},
Expand Down
111 changes: 111 additions & 0 deletions src/components/YamlEditor/YamlEditor.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { React, useState } from "react";

import AceEditor from "react-ace";
import "ace-builds/src-noconflict/theme-github";
import "ace-builds/src-noconflict/ext-language_tools";

import "ace-builds/webpack-resolver";
import "ace-builds/src-noconflict/mode-yaml";

import jsYaml from "js-yaml";

import Ajv from "ajv";
const ajv = new Ajv({ allErrors: true });

import Schema from "../../../static/schema/schema.v2.json";

export default function YamlEditor() {
const [annotations, setAnnotations] = useState([]);
const [value, setValue] = useState(
"# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json\n"
);
const validate = ajv.compile(Schema.definitions.schema);
function getRowFromPath(path) {
// Convert path to row number (0-based)
return path.split("/").length - 1;
}
function getLineNumber(yaml, path) {
const lines = yaml.split("\n");
const pathParts = path.split("/").filter(Boolean);
let currentObj = jsYaml.load(yaml);
let lineNumber = 0;

for (const part of pathParts) {
for (let i = lineNumber; i < lines.length; i++) {
if (lines[i].trim().startsWith(part + ":")) {
lineNumber = i;
break;
}
}
currentObj = currentObj[part];
}

return lineNumber;
}
function onChange(newValue) {
setValue(newValue);
try {
const doc = jsYaml.load(newValue, { strict: true });
const valid = validate(doc);
console.log("Validation result:", valid);
console.log("Validation errors:", validate.errors);

if (!valid && validate.errors) {
setAnnotations(
validate.errors.map((err) => ({
row: err.instancePath
? getLineNumber(newValue, err.instancePath)
: 0,
column: 0,
text: `${err.keyword}: ${err.message} ${
err?.params?.allowedValues
? `Allowed values: ${err.params.allowedValues.join(", ")}`
: ""
}`,
type: "error",
}))
);
} else {
setAnnotations([]);
}
} catch (err) {
console.error("Error:", err);
console.log({
errorMessage: err.reason,
column: err.mark?.column,
line: err.mark?.line,
});
setAnnotations([
{
row: err.instancePath ? getLineNumber(newValue, err.instancePath) : 0,
column: 0,
text:
`${err.keyword}: ${err.message} ${
err?.params?.allowedValues
? `Allowed values: ${err.params.allowedValues.join(", ")}`
: ""
}` || "YAML parsing error",
type: "error",
},
]);
}
}
return (
<AceEditor
mode="yaml"
theme="github"
onChange={onChange}
value={value}
name="yaml-editor"
editorProps={{ $blockScrolling: true }}
setOptions={{
useWorker: false,
showPrintMargin: false,
showGutter: true,
}}
annotations={annotations}
width="100%"
height="400px"
/>
);
}