Skip to content
Closed
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
25 changes: 16 additions & 9 deletions scripts/generate-api-docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,24 @@ async function generateSidebarFile(inputDir, outDir) {
}

async function generateMarkdownFiles(inputDir, outDir) {
return new Promise((resolve, reject) => {
exec(
try {
const { stdout } = await execPromise(
`api-documenter markdown --input-folder ${inputDir} --output-folder ${outDir}`,
(err, stdout) => {
if (err) {
return reject(err);
}

return resolve(stdout);
},
);
return stdout;
} catch (err) {
throw new Error(`Error generating markdown files: ${err.message}`);
}
}

function execPromise(command) {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (err) {
return reject(err);
}
resolve({ stdout, stderr });
});
});
}

Expand Down
111 changes: 27 additions & 84 deletions scripts/generate-api-docs/sidebar-visitor.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,35 @@
const { ApiItemKind } = require('@microsoft/api-extractor-model');

/**
* This is taken from the docusaurus-plugin-api-extractor codebase as specified in the
* [standard-markdown-documenter README](https://github.com/gabrielcsapo/docusaurus-plugin-api-extractor/tree/main/plugin/standard-markdown-documenter#custom-sidebar-visitor).
* @see https://github.com/gabrielcsapo/docusaurus-plugin-api-extractor/tree/main/plugin/standard-markdown-documenter#custom-sidebar-visitor
*
* @dkozma: We had to change this code because the API model parsed an `IndexSignature` from `c2pa.manifestresolvers`
* which gets picked up by the sidebar but a file doesn't get generated. I'm not sure why this is the case or how to
* fix it, so I put a hack in here to just strip the `._indexer_` suffix since standard-markdown-documenter doesn't
* seem to let me return an empty node or node that returns a valid `html` or `link` type.
* @dkozma: The API model parsed an `IndexSignature` from `c2pa.manifestresolvers`, which gets picked up by the sidebar but a file doesn't get generated.
* HACK: Strip the `._indexer_` suffix since standard-markdown-documenter doesn't seem to let
* you return an empty node or node that returns a valid `html` or `link` type.
*/
exports.SIDEBAR_VISITOR = {
[ApiItemKind.Package](apiItem, meta) {
return containerNode(apiItem, meta);
},
[ApiItemKind.Namespace](apiItem, meta) {
return containerNode(apiItem, meta);
},
[ApiItemKind.Interface](apiItem, meta) {
return containerNode(apiItem, meta);
},
[ApiItemKind.Class](apiItem, meta) {
return containerNode(apiItem, meta);
},
[ApiItemKind.CallSignature](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},
[ApiItemKind.ConstructSignature](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},
[ApiItemKind.Constructor](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},
[ApiItemKind.Enum](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.EnumMember](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.Function](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.IndexSignature](apiItem, meta) {
return terminalNode(
apiItem.displayName,
meta.id.replace(/._indexer_$/, ''),
);
},

[ApiItemKind.Method](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.Method](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.MethodSignature](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.Property](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.PropertySignature](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.TypeAlias](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.Variable](apiItem, meta) {
return terminalNode(apiItem.displayName, meta.id);
},

[ApiItemKind.Model]() {
return {
type: 'category',
label: 'Packages',
items: [terminalNode('Overview', 'index')],
collapsed: false,
};
},
[ApiItemKind.Package]: containerNode,
[ApiItemKind.Namespace]: containerNode,
[ApiItemKind.Interface]: containerNode,
[ApiItemKind.Class]: containerNode,
[ApiItemKind.CallSignature]: terminalNode,
[ApiItemKind.ConstructSignature]: terminalNode,
[ApiItemKind.Constructor]: terminalNode,
[ApiItemKind.Enum]: terminalNode,
[ApiItemKind.EnumMember]: terminalNode,
[ApiItemKind.Function]: terminalNode,
[ApiItemKind.IndexSignature]: (apiItem, meta) =>
terminalNode(apiItem.displayName, meta.id.replace(/._indexer_$/, '')),
[ApiItemKind.Method]: terminalNode,
[ApiItemKind.MethodSignature]: terminalNode,
[ApiItemKind.Property]: terminalNode,
[ApiItemKind.PropertySignature]: terminalNode,
[ApiItemKind.TypeAlias]: terminalNode,
[ApiItemKind.Variable]: terminalNode,
[ApiItemKind.Model]: () => ({
type: 'category',
label: 'Packages',
items: [terminalNode('Overview', 'index')],
collapsed: false,
}),
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 elaborate on what's happening here? I wanna make sure I understand
Also, is there a PR in the JS SDK side to move things that we can't make open source?

Copy link
Collaborator Author

@crandmck crandmck Jan 17, 2025

Choose a reason for hiding this comment

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

This code generates the sidebar navigation for the API docs, e.g. for Packages https://opensource.contentauthenticity.org/docs/js-sdk/api/. Currently, there's only one package, c2pa.

The following is from GH co-pilot ;-)

Each key in the SIDEBAR_VISITOR object corresponds to a specific kind of API item, such as Package, Namespace, Interface, Class, and various others. The associated function for each key takes two parameters: apiItem, which represents the API item being processed, and meta, which contains metadata about the item. Depending on the kind of API item, the function either calls containerNode or terminalNode to generate the sidebar node.

Also, is there a PR in the JS SDK side to move things that we can't make open source?

The JS SDK is already open source: https://github.com/contentauth/c2pa-js
This PR doesn't do anything re that; it's only for the the code in this repo, which creates the docs.

Copy link
Contributor

Choose a reason for hiding this comment

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

so just reformatting right?
and apiItem and meta are passed into the larger function?

};

function containerNode(apiItem, meta) {
Expand Down
Loading