mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-01-07 01:09:38 -06:00
* feat: universal tabs * fix: consistent tab naming * feat: paths * wip: chunk parser * wip: dynamic snips * wip: components * docs: annotated go * docs: improved annotations * chore: fix * feat: on-failure * todo: switch branch to main * chore: lint * chore: lint * fix: resolved theme * cache * fix: theme again... * todo list * docs: shadcn * docs: code example uses shadcn * fix: build * python note * chore: lint * chore: swap main * feat: local story * fix: is dev * revert the change that broke vercel * fix: redirect? * feat: seo title
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
const commentPrefixes = ['//', '#'];
|
|
|
|
const isCommentLine = (line: string) => {
|
|
const trimmed = line.trim();
|
|
return commentPrefixes.some(prefix => trimmed.startsWith(prefix));
|
|
}
|
|
|
|
const startsWithPrefixAndChar = (line: string, specialChar: string) => {
|
|
const trimmed = line.trim();
|
|
return commentPrefixes.some(prefix => trimmed.startsWith(`${prefix} ${specialChar}`));
|
|
}
|
|
|
|
const isTargetLine = (line: string, target: string) => {
|
|
const split = line.split('❓');
|
|
return split[1].trim() === target.trim();
|
|
}
|
|
|
|
|
|
export const parseDocComments = (
|
|
source: string,
|
|
target: string,
|
|
collapsed: boolean = false,
|
|
): string => {
|
|
const lines = source.split('\n');
|
|
let isSnippet = false;
|
|
let isCollecting = false;
|
|
const resultLines: string[] = [];
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const line = lines[i];
|
|
|
|
// Start collecting at ❓
|
|
if (isCommentLine(line) && startsWithPrefixAndChar(line, '❓') && isTargetLine(line, target)) {
|
|
isSnippet = true;
|
|
isCollecting = true;
|
|
continue;
|
|
}
|
|
|
|
if (isSnippet && isCommentLine(line)) {
|
|
// Start collecting again at 👀
|
|
if (startsWithPrefixAndChar(line, '👀')) {
|
|
isCollecting = true;
|
|
}
|
|
|
|
// Start collecting again at ,
|
|
if (startsWithPrefixAndChar(line, ',')) {
|
|
isCollecting = true;
|
|
continue;
|
|
}
|
|
|
|
// Collect until '...'
|
|
if (isCollecting && startsWithPrefixAndChar(line, '...')) {
|
|
if (collapsed) {
|
|
isCollecting = false;
|
|
resultLines.push(line);
|
|
} else {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Stop at ‼️
|
|
if (isSnippet && line.includes('‼️')) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Collect focused section
|
|
if (isCollecting) {
|
|
if (line.trim() === '') {
|
|
resultLines.push(' ');
|
|
} else {
|
|
resultLines.push(line);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resultLines.length === 0) {
|
|
return `🚨 No snippet found for ${target} \n\n${source}`;
|
|
}
|
|
|
|
return resultLines.join('\n');
|
|
};
|