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 data category and update comments to support codeBlocks #8015

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions src/components/ApiDocs/ApiComment.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Fragment } from 'react';
import { View } from '@aws-amplify/ui-react';
import { parseMarkdownLinks, parseMarkdown } from '@/utils/parseMdxLinks';
import { MDXCode } from '../MDXComponents';

interface ApiCommentProps {
apiComment?: any[];
Expand All @@ -15,6 +16,23 @@ export const ApiComment = ({ apiComment, codeBlock }: ApiCommentProps) => {
}
const commentList = apiComment.map((snippet, idx) => {
if (snippet.kind === 'code') {
if (snippet.text.indexOf('```') !== -1) {
// If the comment contains a code block extract the language and render an MDXCode component
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give an example of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the current use case

{
      "kind": "code",
      "text": "```ts\nimport { DataStore, syncExpression } from 'aws-amplify/datastore';\nimport { Post, Comment } from './models';\n\n\nDataStore.configure({\n\tsyncExpressions: [\n\t\tsyncExpression(Post, () => {\n\t\t\treturn (post) => post.rating.gt(5);\n\t\t}),\n\t\tsyncExpression(Comment, () => {\n\t\t\treturn (comment) => comment.status.eq('active');\n\t\t})\n\t]\n});\n```"
}

The comment node is marked as code but the text inside the comment could be either a code block or just a singular code tag, we are using ``` to differentiate the code blocks from the ` code tags

let text = snippet.text;
const regex = /(?<=```)([^\n```]*)/;
const lang = text.match(regex)[0];
if (lang) {
text = text.replaceAll('```' + lang, '').replaceAll('```', '');
}
return (
<MDXCode
key={idx}
codeString={text}
showLineNumbers={false}
language={lang}
/>
);
}
return <code key={idx}>{snippet.text.replaceAll('`', '')}</code>;
} else {
const text = snippet.text;
Expand Down
13 changes: 8 additions & 5 deletions src/components/ApiDocs/display/ParameterType.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { View } from '@aws-amplify/ui-react';
import { TypeLink } from './TypeLink';
import { LinkDataType } from './TypeLink';
import React from 'react';
import references from '@/directory/apiReferences.json';

interface typeDataType {
typeArguments?: LinkDataType[];
typeArguments?: typeDataType[];
type: string | typeDataType;
kind: number;
types?: [];
Expand Down Expand Up @@ -43,15 +42,19 @@ export const ParameterType = ({ typeData }: ParameterComponentType) => {

// adds type arguments to an array to be rendered
const addTypeArgs = (
typeArgs: LinkDataType[],
typeArgs: typeDataType[],
displayArray: TypeArguments
): TypeArguments => {
const typeArgArray = typeArgs.reduce<TypeArguments>((acc, tArg, index) => {
let retValue;
if (index === 0) {
retValue = [<TypeLink key={tArg.name} linkData={tArg} />];
retValue = [<ParameterType key={tArg.name} typeData={tArg} />];
} else {
retValue = [...acc, ', ', <TypeLink key={tArg.name} linkData={tArg} />];
retValue = [
...acc,
', ',
<ParameterType key={tArg.name} typeData={tArg} />
];
}
if (tArg?.typeArguments?.length) {
addTypeArgs(tArg.typeArguments, retValue);
Expand Down
3 changes: 2 additions & 1 deletion src/data/api-categories.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// mapping of api categories coming in from libraries to the associated categories in the docs
export const API_CATEGORIES = {
auth: 'auth',
storage: 'storage'
storage: 'storage',
data: 'datastore'
};

export const API_SUB_CATEGORIES = {
Expand Down
Loading