Skip to content

Commit

Permalink
Improve placeholder name handling to meet IDX restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Nov 23, 2024
1 parent 8d2cacb commit 31ca94b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/pages/[...rest].ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ type CachedExample = {
const examplesCache = new Map<string, CachedExample[]>();
let starlightExamplesCache: CachedExample[] | undefined = undefined;

/** Generate a placeholder workspace name for IDX. Must be no longer than 20 characters. */
function idxProjectName(example: ExampleData, repo: string) {
const fullTitle = toTitle(
repo === 'withastro/starlight' ? toStarlightName(example.name) : example.name,
)
// Remove parentheticals
.replace(/\([^)]+\)/, '')
.trim();

if (fullTitle.length > 20) return `${fullTitle.slice(0, 19)}…`;
if (fullTitle.length < 13) return `Astro: ${fullTitle}`;
return fullTitle;
}

/**
* Generate a URL to create a new IDX workspace for the given example.
*
Expand All @@ -34,7 +48,7 @@ function idxUrl(example: ExampleData, repo: string, ref = 'latest') {
// Select the Astro template to use when starting up IDX.
url.searchParams.set('astroTemplate', toTemplateName({ ...example, repo }));
// Pre-fill the IDX wizard with a project name based on the selected template.
const title = `Astro: ${toTitle(repo === 'withastro/starlight' ? toStarlightName(example.name) : example.name)}`;
const title = idxProjectName(example, repo);
url.searchParams.set('name', title);
// Tell IDX where the template files are located. IDX parses this greedily so it MUST COME LAST.
const templateUrl = `https://github.com/withastro/astro.new/tree/main/.idx-templates/${ref}`;
Expand Down

0 comments on commit 31ca94b

Please sign in to comment.