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

Add a yaml format util for handling formatting #65

Merged
merged 1 commit into from
Jul 11, 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
6 changes: 4 additions & 2 deletions src/app/api/pr/knowledge/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
import { NextRequest } from 'next/server';
import yaml from 'js-yaml';
import { KnowledgeYamlData, AttributionData, YamlLineLength } from '@/types';
import { KnowledgeYamlData, AttributionData } from '@/types';
import { dumpYaml } from '@/utils/yamlConfig';

const GITHUB_API_URL = 'https://api.github.com';
const UPSTREAM_REPO_OWNER = process.env.NEXT_PUBLIC_TAXONOMY_REPO_OWNER!;
Expand Down Expand Up @@ -51,7 +52,8 @@ export async function POST(req: NextRequest) {
const newYamlFilePath = `${filePath}qna.yaml`;
const newAttributionFilePath = `${filePath}attribution.txt`;

const yamlString = yaml.dump(knowledgeData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(knowledgeData);

const attributionContent = `Title of work: ${attributionData.title_of_work}
Link to work: ${attributionData.link_to_work}
Revision: ${attributionData.revision}
Expand Down
5 changes: 3 additions & 2 deletions src/app/api/pr/skill/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { NextResponse } from 'next/server';
import { getToken } from 'next-auth/jwt';
import { NextRequest } from 'next/server';
import yaml from 'js-yaml';
import { YamlLineLength, SkillYamlData, AttributionData } from '@/types';
import { SkillYamlData, AttributionData } from '@/types';
import { dumpYaml } from '@/utils/yamlConfig';

const GITHUB_API_URL = 'https://api.github.com';
const UPSTREAM_REPO_OWNER = process.env.NEXT_PUBLIC_TAXONOMY_REPO_OWNER!;
Expand Down Expand Up @@ -47,7 +48,7 @@ export async function POST(req: NextRequest) {
const skillData = yaml.load(content) as SkillYamlData;
const attributionData = attribution as AttributionData;

const yamlString = yaml.dump(skillData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(skillData);

const attributionString = `Title of work: ${attributionData.title_of_work}
Link to work: ${attributionData.link_to_work}
Expand Down
11 changes: 5 additions & 6 deletions src/app/edit-submission/knowledge/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Button } from '@patternfly/react-core/dist/dynamic/components/Button';
import { Text } from '@patternfly/react-core/dist/dynamic/components/Text';
import { AppLayout } from '../../../../components/AppLayout';
import { UploadFile } from '../../../../components/Contribute/Knowledge/UploadFile';
import { AttributionData, PullRequestFile, KnowledgeYamlData, SchemaVersion, YamlLineLength } from '@/types';
import { AttributionData, PullRequestFile, KnowledgeYamlData, SchemaVersion } from '@/types';
import {
fetchPullRequest,
fetchFileContent,
Expand All @@ -29,6 +29,7 @@ import {
} from '../../../../utils/github';
import yaml from 'js-yaml';
import axios from 'axios';
import { dumpYaml } from '@/utils/yamlConfig';

const UPSTREAM_REPO_NAME = process.env.NEXT_PUBLIC_TAXONOMY_REPO!;

Expand Down Expand Up @@ -166,11 +167,9 @@ const EditKnowledgePage: React.FunctionComponent<{ params: { id: string } }> = (
answer: answers[index]
}))
};
const updatedYamlContent = yaml.dump(updatedYamlData, {
lineWidth: YamlLineLength,
noCompatMode: true,
quotingType: '"'
});

const updatedYamlContent = dumpYaml(updatedYamlData);

console.log('Updated YAML content:', updatedYamlContent);

const updatedAttributionData: AttributionData = {
Expand Down
10 changes: 4 additions & 6 deletions src/app/edit-submission/skill/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form
import { Button } from '@patternfly/react-core/dist/dynamic/components/Button';
import { Text } from '@patternfly/react-core/dist/dynamic/components/Text';
import { AppLayout } from '../../../../components/AppLayout';
import { AttributionData, PullRequestFile, SchemaVersion, SkillYamlData, YamlLineLength } from '@/types';
import { AttributionData, PullRequestFile, SchemaVersion, SkillYamlData } from '@/types';
import {
fetchPullRequest,
fetchFileContent,
Expand All @@ -28,6 +28,7 @@ import {
} from '../../../../utils/github';
import yaml from 'js-yaml';
import axios from 'axios';
import { dumpYaml } from '@/utils/yamlConfig';

const UPSTREAM_REPO_NAME = process.env.NEXT_PUBLIC_TAXONOMY_REPO!;

Expand Down Expand Up @@ -148,11 +149,8 @@ const EditSkillPage: React.FunctionComponent<{ params: { id: string } }> = ({ pa
answer: answers[index]
}))
};
const updatedYamlContent = yaml.dump(updatedYamlData, {
lineWidth: YamlLineLength,
noCompatMode: true,
quotingType: '"'
});

const updatedYamlContent = dumpYaml(updatedYamlData);

console.log('Updated YAML content:', updatedYamlContent);

Expand Down
10 changes: 5 additions & 5 deletions src/components/Contribute/Knowledge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { Form } from '@patternfly/react-core/dist/dynamic/components/Form';
import { FormGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
import { TextArea } from '@patternfly/react-core/dist/dynamic/components/TextArea';
import { PlusIcon, MinusCircleIcon, CodeIcon } from '@patternfly/react-icons/dist/dynamic/icons/';
import yaml from 'js-yaml';
import { validateFields, validateEmail, validateUniqueItems } from '../../../utils/validation';
import { getGitHubUsername } from '../../../utils/github';
import { useSession } from 'next-auth/react';
import YamlCodeModal from '../../YamlCodeModal';
import { UploadFile } from './UploadFile';
import { SchemaVersion, YamlLineLength, KnowledgeYamlData, AttributionData } from '@/types';
import { SchemaVersion, KnowledgeYamlData, AttributionData } from '@/types';
import KnowledgeDescription from './KnowledgeDescription';
import { dumpYaml } from '@/utils/yamlConfig';

export const KnowledgeForm: React.FunctionComponent = () => {
const { data: session } = useSession();
Expand Down Expand Up @@ -204,7 +204,7 @@ export const KnowledgeForm: React.FunctionComponent = () => {
}
};

const yamlString = yaml.dump(knowledgeData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(knowledgeData);

const attributionData: AttributionData = {
title_of_work: title_work,
Expand Down Expand Up @@ -364,7 +364,7 @@ export const KnowledgeForm: React.FunctionComponent = () => {
}
};

const yamlString = yaml.dump(yamlData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(yamlData);
const blob = new Blob([yamlString], { type: 'application/x-yaml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
Expand Down Expand Up @@ -420,7 +420,7 @@ Creator names: ${creators}
}
};

const yamlString = yaml.dump(yamlData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(yamlData);
setYamlContent(yamlString);
setIsModalOpen(true);
};
Expand Down
1 change: 0 additions & 1 deletion src/components/Contribute/Knowledge/knowledge.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
margin-bottom: 50px;
}


.submit-k:hover,
.download-k-yaml:hover,
.download-k-attribution:hover,
Expand Down
10 changes: 5 additions & 5 deletions src/components/Contribute/Skill/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { FormGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
import { TextArea } from '@patternfly/react-core/dist/dynamic/components/TextArea';
import { PlusIcon, MinusCircleIcon, CodeIcon } from '@patternfly/react-icons/dist/dynamic/icons/';
import { validateFields, validateEmail, validateUniqueItems } from '../../../utils/validation';
import yaml from 'js-yaml';
import { getGitHubUsername } from '../../../utils/github';
import { useSession } from 'next-auth/react';
import YamlCodeModal from '../../YamlCodeModal';
import { AttributionData, SchemaVersion, SkillYamlData, YamlLineLength } from '@/types';
import { AttributionData, SchemaVersion, SkillYamlData } from '@/types';
import SkillDescription from './SkillDescription';
import { dumpYaml } from '@/utils/yamlConfig';

export const SkillForm: React.FunctionComponent = () => {
const { data: session } = useSession();
Expand Down Expand Up @@ -188,7 +188,7 @@ export const SkillForm: React.FunctionComponent = () => {
}))
};

const yamlString = yaml.dump(skillData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(skillData);

const attributionData: AttributionData = {
title_of_work: title_work,
Expand Down Expand Up @@ -237,7 +237,7 @@ export const SkillForm: React.FunctionComponent = () => {
}))
};

const yamlString = yaml.dump(yamlData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(yamlData);

setYamlContent(yamlString);
setIsModalOpen(true);
Expand Down Expand Up @@ -309,7 +309,7 @@ export const SkillForm: React.FunctionComponent = () => {
})
};

const yamlString = yaml.dump(yamlData, { lineWidth: YamlLineLength });
const yamlString = dumpYaml(yamlData);
const blob = new Blob([yamlString], { type: 'application/x-yaml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
Expand Down
1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// src/types/index.ts

export const SchemaVersion = 2;
export const YamlLineLength = 80;

export interface Endpoint {
id: string;
Expand Down
44 changes: 44 additions & 0 deletions src/utils/yamlConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// src/app/utils/yamlConfig.ts
import yaml from 'js-yaml';

export const YamlLineLength = 80;

export const EmptyStringYamlType = new yaml.Type('!empty', {
kind: 'scalar',
resolve: (data: unknown): boolean => data === '' || data === null,
represent: (): string => '',
predicate: (object: unknown): boolean => object === '' || object === null
});

export const CustomYamlSchema = yaml.DEFAULT_SCHEMA.extend({ implicit: [EmptyStringYamlType] });

export interface YamlDumpOptions extends yaml.DumpOptions {
schema: typeof CustomYamlSchema;
lineWidth: number;
noQuotes?: boolean;
}

export const defaultYamlDumpOptions: YamlDumpOptions = {
schema: CustomYamlSchema,
lineWidth: YamlLineLength,
noQuotes: true
};

function removeNullValues(obj: unknown): unknown {
if (Array.isArray(obj)) {
return obj.map(removeNullValues);
} else if (obj !== null && typeof obj === 'object') {
return Object.fromEntries(
Object.entries(obj)
.map(([key, value]) => [key, removeNullValues(value)])
.filter(([, value]) => value !== null)
);
}
return obj === null ? '' : obj;
}

export function dumpYaml(data: unknown, options: Partial<YamlDumpOptions> = {}): string {
const mergedOptions: YamlDumpOptions = { ...defaultYamlDumpOptions, ...options };
const processedData = removeNullValues(data);
return yaml.dump(processedData, mergedOptions);
}