Skip to content

Commit

Permalink
list item stuff
Browse files Browse the repository at this point in the history
Signed-off-by: flakey5 <[email protected]>
  • Loading branch information
flakey5 committed Sep 9, 2024
1 parent a6cfff7 commit d8d9587
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 55 deletions.
24 changes: 24 additions & 0 deletions src/generators/legacy-json/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Denotes a method's return value
*/
export const RETURN_EXPRESSION = /^returns?\s*:?\s*/i;

/**
* Denotes a method's name
*/
export const NAME_EXPRESSION = /^['`"]?([^'`": {]+)['`"]?\s*:?\s*/;

/**
* Denotes a method's type
*/
export const TYPE_EXPRESSION = /^\{([^}]+)\}\s*/;

/**
* Is there a leading hyphen
*/
export const LEADING_HYPHEN = /^-\s*/;

/**
* Denotes a default value
*/
export const DEFAULT_EXPRESSION = /\s*\*\*Default:\*\*\s*([^]+)$/i;
11 changes: 11 additions & 0 deletions src/generators/legacy-json/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ListItem } from 'mdast';

export interface HierarchizedEntry extends ApiDocMetadataEntry {
hierarchyChildren: ApiDocMetadataEntry[];
}
Expand Down Expand Up @@ -61,3 +63,12 @@ export interface MiscSection extends SectionBase {
type: 'misc';
[key: string]: string | undefined;
}

export interface List {
textRaw: string;
desc?: string;
name: string;
type?: string;
default?: string;
options?: List;
}
25 changes: 20 additions & 5 deletions src/generators/legacy-json/utils/buildHierarchy.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/**
* So we need the files to be in a hierarchy based off of depth, but they're
* given to us flattened. So, let's fix that in a way that's incredibly
* unfortunate but works!
* @param {ApiDocMetadataEntry[]} entries
* @returns {import('../types.d.ts').HierarchizedEntry[]}
* given to us flattened. So, let's fix that.
*
* Assuming that {@link entries} is in the same order as the elements are in
* the markdown, we can use the entry's depth property to reassemble the
* hierarchy.
*
* If depth <= 1, it's a top-level element (aka a root).
*
* If it's depth is greater than the previous entry's depth, it's a child of
* the previous entry. Otherwise (if it's less than or equal to the previous
* entry's depth), we need to find the entry that it was the greater than. We
* can do this by just looping through entries in reverse starting at the
* current index - 1.
*
* @param {Array<ApiDocMetadataEntry>} entries
* @returns {Array<import('../types.d.ts').HierarchizedEntry>}
*/
export function buildHierarchy(entries) {
const roots = [];
Expand All @@ -12,7 +24,8 @@ export function buildHierarchy(entries) {
const entry = entries[i];
const currentDepth = entry.heading.depth;

if (currentDepth === 1) {
// We're a top-level entry
if (currentDepth <= 1) {
roots.push(entry);
continue;
}
Expand All @@ -27,10 +40,12 @@ export function buildHierarchy(entries) {
}
previousEntry.hierarchyChildren.push(entry);
} else {
// Loop to find the entry we're a child of
for (let j = i - 2; j >= 0; j--) {
const jEntry = entries[j];
const jDepth = jEntry.heading.depth;
if (currentDepth > jDepth) {
// Found it
jEntry.hierarchyChildren.push(entry);
break;
}
Expand Down
Loading

0 comments on commit d8d9587

Please sign in to comment.