Skip to content

Commit 907cb6d

Browse files
[getsentry/action-github-commit] Auto commit
1 parent 673388c commit 907cb6d

File tree

6 files changed

+100
-54
lines changed

6 files changed

+100
-54
lines changed

src/components/apiExamples/apiExamples.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ export function ApiExamples({api}: Props) {
7777
if (!codeContext) {
7878
return null;
7979
}
80-
80+
8181
const {codeKeywords, sharedKeywordSelection} = codeContext;
8282
const [sharedSelection] = sharedKeywordSelection;
83-
const currentSelectionIdx = sharedSelection['PROJECT'] ?? 0;
83+
const currentSelectionIdx = sharedSelection.PROJECT ?? 0;
8484
const currentProject = codeKeywords?.PROJECT?.[currentSelectionIdx];
85-
85+
8686
return currentProject?.title;
8787
};
8888

src/components/codeBlock/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
5353
if (!codeContext) {
5454
return null;
5555
}
56-
56+
5757
const {codeKeywords, sharedKeywordSelection} = codeContext;
5858
const [sharedSelection] = sharedKeywordSelection;
59-
const currentSelectionIdx = sharedSelection['PROJECT'] ?? 0;
59+
const currentSelectionIdx = sharedSelection.PROJECT ?? 0;
6060
const currentProject = codeKeywords?.PROJECT?.[currentSelectionIdx];
61-
61+
6262
return currentProject?.title;
6363
};
6464

src/components/codeKeywords/keywordSelector.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ export function KeywordSelector({keyword, group, index}: KeywordSelectorProps) {
7171
}
7272

7373
// Enhanced tooltip text that makes it clear users can change projects
74-
const tooltipText = choices.length > 1
75-
? `Current project: ${currentSelection?.title}. Click to select a different project.`
76-
: `Current project: ${currentSelection?.title}`;
74+
const tooltipText =
75+
choices.length > 1
76+
? `Current project: ${currentSelection?.title}. Click to select a different project.`
77+
: `Current project: ${currentSelection?.title}`;
7778

7879
const selector = isOpen && (
7980
<PositionWrapper style={styles.popper} ref={setDropdownEl} {...attributes.popper}>
@@ -138,7 +139,7 @@ export function KeywordSelector({keyword, group, index}: KeywordSelectorProps) {
138139
// Add subtle visual cues to indicate this is clickable
139140
cursor: 'pointer',
140141
borderBottom: choices.length > 1 ? '1px dotted currentColor' : undefined,
141-
position: 'relative'
142+
position: 'relative',
142143
}}
143144
>
144145
<KeywordIndicatorComponent isOpen={isOpen} />
@@ -162,12 +163,12 @@ export function KeywordSelector({keyword, group, index}: KeywordSelectorProps) {
162163
</span>
163164
{/* Add a small indicator when multiple projects are available */}
164165
{choices.length > 1 && (
165-
<span
166+
<span
166167
style={{
167168
fontSize: '0.75em',
168169
opacity: 0.6,
169170
marginLeft: '2px',
170-
userSelect: 'none'
171+
userSelect: 'none',
171172
}}
172173
title="Click to change project"
173174
>

src/files.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {readdir} from 'fs/promises';
22
import path from 'path';
3+
34
import pLimit from 'p-limit';
45

56
/**

src/mdx.ts

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import {BinaryLike, createHash} from 'crypto';
2-
3-
import {cache} from 'react';
41
import matter from 'gray-matter';
52
import {s} from 'hastscript';
63
import yaml from 'js-yaml';
74
import {bundleMDX} from 'mdx-bundler';
5+
import {BinaryLike, createHash} from 'node:crypto';
86
import {createReadStream, createWriteStream, mkdirSync} from 'node:fs';
9-
import {access, opendir, readFile} from 'node:fs/promises';
7+
import {access, cp, mkdir, opendir, readFile} from 'node:fs/promises';
108
import path from 'node:path';
119
// @ts-expect-error ts(2305) -- For some reason "compose" is not recognized in the types
1210
import {compose, Readable} from 'node:stream';
@@ -34,6 +32,7 @@ import rehypeSlug from './rehype-slug.js';
3432
import remarkCodeTabs from './remark-code-tabs';
3533
import remarkCodeTitles from './remark-code-title';
3634
import remarkComponentSpacing from './remark-component-spacing';
35+
import remarkDsnComments from './remark-dsn-comments';
3736
import remarkExtractFrontmatter from './remark-extract-frontmatter';
3837
import remarkFormatCodeBlocks from './remark-format-code';
3938
import remarkImageSize from './remark-image-size';
@@ -42,7 +41,6 @@ import remarkVariables from './remark-variables';
4241
import {FrontMatter, Platform, PlatformConfig} from './types';
4342
import {isNotNil} from './utils';
4443
import {isVersioned, VERSION_INDICATOR} from './versioning';
45-
import remarkDsnComments from './remark-dsn-comments';
4644

4745
type SlugFile = {
4846
frontMatter: Platform & {slug: string};
@@ -74,14 +72,15 @@ async function readCacheFile<T>(file: string): Promise<T> {
7472
}
7573

7674
async function writeCacheFile(file: string, data: string) {
75+
const bufferData = Buffer.from(data);
7776
await pipeline(
78-
Readable.from(data),
77+
Readable.from(bufferData),
7978
createBrotliCompress({
8079
chunkSize: 32 * 1024,
8180
params: {
8281
[zlibConstants.BROTLI_PARAM_MODE]: zlibConstants.BROTLI_MODE_TEXT,
8382
[zlibConstants.BROTLI_PARAM_QUALITY]: CACHE_COMPRESS_LEVEL,
84-
[zlibConstants.BROTLI_PARAM_SIZE_HINT]: data.length,
83+
[zlibConstants.BROTLI_PARAM_SIZE_HINT]: bufferData.length,
8584
},
8685
}),
8786
createWriteStream(file)
@@ -523,17 +522,33 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
523522
);
524523
}
525524

526-
const cacheKey = md5(source);
527-
const cacheFile = path.join(CACHE_DIR, cacheKey);
525+
let cacheKey: string | null = null;
526+
let cacheFile: string | null = null;
527+
let assetsCacheDir: string | null = null;
528+
const outdir = path.join(root, 'public', 'mdx-images');
529+
await mkdir(outdir, {recursive: true});
528530

529-
try {
530-
const cached = await readCacheFile<SlugFile>(cacheFile);
531-
return cached;
532-
} catch (err) {
533-
if (err.code !== 'ENOENT' && err.code !== 'ABORT_ERR') {
534-
// If cache is corrupted, ignore and proceed
535-
// eslint-disable-next-line no-console
536-
console.warn(`Failed to read MDX cache: ${cacheFile}`, err);
531+
if (process.env.CI) {
532+
cacheKey = md5(source);
533+
cacheFile = path.join(CACHE_DIR, `${cacheKey}.br`);
534+
assetsCacheDir = path.join(CACHE_DIR, cacheKey);
535+
536+
try {
537+
const [cached, _] = await Promise.all([
538+
readCacheFile<SlugFile>(cacheFile),
539+
cp(assetsCacheDir, outdir, {recursive: true}),
540+
]);
541+
return cached;
542+
} catch (err) {
543+
if (
544+
err.code !== 'ENOENT' &&
545+
err.code !== 'ABORT_ERR' &&
546+
err.code !== 'Z_BUF_ERROR'
547+
) {
548+
// If cache is corrupted, ignore and proceed
549+
// eslint-disable-next-line no-console
550+
console.warn(`Failed to read MDX cache: ${cacheFile}`, err);
551+
}
537552
}
538553
}
539554

@@ -632,8 +647,12 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
632647
'.svg': 'dataurl',
633648
};
634649
// Set the `outdir` to a public location for this bundle.
635-
// this where this images will be copied
636-
options.outdir = path.join(root, 'public', 'mdx-images');
650+
// this is where these images will be copied
651+
// the reason we use the cache folder when it's
652+
// enabled is because mdx-images is a dumping ground
653+
// for all images, so we cannot filter it out only
654+
// for this specific slug easily
655+
options.outdir = assetsCacheDir || outdir;
637656

638657
// Set write to true so that esbuild will output the files.
639658
options.write = true;
@@ -663,17 +682,30 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
663682
},
664683
};
665684

666-
writeCacheFile(cacheFile, JSON.stringify(resultObj)).catch(e => {
667-
// eslint-disable-next-line no-console
668-
console.warn(`Failed to write MDX cache: ${cacheFile}`, e);
669-
});
685+
if (assetsCacheDir && cacheFile) {
686+
await cp(assetsCacheDir, outdir, {recursive: true});
687+
writeCacheFile(cacheFile, JSON.stringify(resultObj)).catch(e => {
688+
// eslint-disable-next-line no-console
689+
console.warn(`Failed to write MDX cache: ${cacheFile}`, e);
690+
});
691+
}
670692

671693
return resultObj;
672694
}
673695

696+
const fileBySlugCache = new Map<string, Promise<SlugFile>>();
697+
674698
/**
675699
* Cache the result of {@link getFileBySlug}.
676700
*
677701
* This is useful for performance when rendering the same file multiple times.
678702
*/
679-
export const getFileBySlugWithCache = cache(getFileBySlug);
703+
export function getFileBySlugWithCache(slug: string): Promise<SlugFile> {
704+
let cached = fileBySlugCache.get(slug);
705+
if (!cached) {
706+
cached = getFileBySlug(slug);
707+
fileBySlugCache.set(slug, cached);
708+
}
709+
710+
return cached;
711+
}

src/remark-dsn-comments.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ export default function remarkDsnComments() {
1515
// Add comment above DSN based on language
1616
const language = node.lang || '';
1717
let comment = '';
18-
18+
1919
switch (language) {
2020
case 'javascript':
2121
case 'typescript':
2222
case 'jsx':
2323
case 'tsx':
24-
comment = '// Hover over the DSN to see your project, or click it to select a different one';
24+
comment =
25+
'// Hover over the DSN to see your project, or click it to select a different one';
2526
break;
2627
case 'python':
2728
case 'ruby':
2829
case 'shell':
2930
case 'bash':
30-
comment = '# Hover over the DSN to see your project, or click it to select a different one';
31+
comment =
32+
'# Hover over the DSN to see your project, or click it to select a different one';
3133
break;
3234
case 'java':
3335
case 'kotlin':
@@ -36,37 +38,46 @@ export default function remarkDsnComments() {
3638
case 'csharp':
3739
case 'c':
3840
case 'cpp':
39-
comment = '// Hover over the DSN to see your project, or click it to select a different one';
41+
comment =
42+
'// Hover over the DSN to see your project, or click it to select a different one';
4043
break;
4144
case 'php':
42-
comment = '// Hover over the DSN to see your project, or click it to select a different one';
45+
comment =
46+
'// Hover over the DSN to see your project, or click it to select a different one';
4347
break;
4448
case 'go':
45-
comment = '// Hover over the DSN to see your project, or click it to select a different one';
49+
comment =
50+
'// Hover over the DSN to see your project, or click it to select a different one';
4651
break;
4752
case 'rust':
48-
comment = '// Hover over the DSN to see your project, or click it to select a different one';
53+
comment =
54+
'// Hover over the DSN to see your project, or click it to select a different one';
4955
break;
5056
case 'yaml':
5157
case 'yml':
52-
comment = '# Hover over the DSN to see your project, or click it to select a different one';
58+
comment =
59+
'# Hover over the DSN to see your project, or click it to select a different one';
5360
break;
5461
case 'toml':
55-
comment = '# Hover over the DSN to see your project, or click it to select a different one';
62+
comment =
63+
'# Hover over the DSN to see your project, or click it to select a different one';
5664
break;
5765
case 'html':
5866
case 'xml':
59-
comment = '<!-- Hover over the DSN to see your project, or click it to select a different one -->';
67+
comment =
68+
'<!-- Hover over the DSN to see your project, or click it to select a different one -->';
6069
break;
6170
case 'css':
62-
comment = '/* Hover over the DSN to see your project, or click it to select a different one */';
71+
comment =
72+
'/* Hover over the DSN to see your project, or click it to select a different one */';
6373
break;
6474
case 'json':
6575
// JSON doesn't support comments, so we skip it
6676
return;
6777
default:
6878
// For unknown languages, try to use a common comment style
69-
comment = '// Hover over the DSN to see your project, or click it to select a different one';
79+
comment =
80+
'// Hover over the DSN to see your project, or click it to select a different one';
7081
break;
7182
}
7283

@@ -78,12 +89,13 @@ export default function remarkDsnComments() {
7889
if (DSN_PATTERN.test(lines[i])) {
7990
// Reset regex for next potential match
8091
DSN_PATTERN.lastIndex = 0;
81-
92+
8293
// Check that we don't already have a comment above this line
83-
const commentAlreadyExists = i > 0 &&
84-
(lines[i - 1].includes('Hover over the DSN') ||
85-
lines[i - 1].includes('hover over the dsn'));
86-
94+
const commentAlreadyExists =
95+
i > 0 &&
96+
(lines[i - 1].includes('Hover over the DSN') ||
97+
lines[i - 1].includes('hover over the dsn'));
98+
8799
if (!commentAlreadyExists) {
88100
// Insert comment before the DSN line
89101
lines.splice(i, 0, comment);
@@ -98,4 +110,4 @@ export default function remarkDsnComments() {
98110
}
99111
});
100112
};
101-
}
113+
}

0 commit comments

Comments
 (0)