Skip to content

Commit

Permalink
Fix encoding/decoding of srcmd due to tsconfig in metadata (srcbookde…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjreinhart authored Aug 15, 2024
1 parent 67c783f commit d07230f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
16 changes: 15 additions & 1 deletion packages/api/srcmd/decoding.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
TitleCellType,
} from '@srcbook/shared';
import type { DecodeCellsResult, DecodeResult } from './types.mjs';
import { toFormattedJSON } from '../utils.mjs';

/**
* This is used to decode a complete .src.md file.
Expand Down Expand Up @@ -93,9 +94,22 @@ function getSrcbookMetadata(tokens: TokensList) {

try {
const metadata = JSON.parse(match[1]);

const filteredTokens = tokens.filter((t) => t !== srcbookMetdataToken);

if (metadata && metadata['tsconfig.json']) {
// This needs to be a string.
const tsconfig = toFormattedJSON(metadata['tsconfig.json']);

return {
metadata: SrcbookMetadataSchema.parse({ ...metadata, 'tsconfig.json': tsconfig }),
tokens: filteredTokens,
};
}

return {
metadata: SrcbookMetadataSchema.parse(metadata),
tokens: tokens.filter((t) => t !== srcbookMetdataToken),
tokens: filteredTokens,
};
} catch (e) {
throw new Error(`Unable to parse srcbook metadata: ${(e as Error).message}`);
Expand Down
28 changes: 22 additions & 6 deletions packages/api/srcmd/encoding.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
TitleCellType,
PlaceholderCellType,
CellWithPlaceholderType,
CodeLanguageType,
} from '@srcbook/shared';
import type { SrcbookType } from './types.mjs';

Expand All @@ -18,13 +19,8 @@ export function encode(srcbook: SrcbookWithPlacebolderType, options: { inline: b
const packageJsonCell = secondCell as PackageJsonCellType;
const cells = remainingCells as (MarkdownCellType | CodeCellType | PlaceholderCellType)[];

const metadata =
srcbook.language === 'javascript'
? { language: srcbook.language }
: { language: srcbook.language, 'tsconfig.json': srcbook['tsconfig.json'] };

const encoded = [
`<!-- srcbook:${JSON.stringify(metadata)} -->`,
encodeMetdata(srcbook),
encodeTitleCell(titleCell),
encodePackageJsonCell(packageJsonCell, options),
...cells.map((cell) => {
Expand All @@ -43,6 +39,26 @@ export function encode(srcbook: SrcbookWithPlacebolderType, options: { inline: b
return encoded.join('\n\n').trimEnd() + '\n';
}

function encodeMetdata(srcbook: SrcbookWithPlacebolderType) {
const metadata: { language: CodeLanguageType; 'tsconfig.json'?: any } = {
language: srcbook.language,
};

// tsconfig is kept as a string in srcbook. However, when encoding
// it in srcmd, we need it to be an object in the metadata header.
if (srcbook.language === 'typescript' && srcbook['tsconfig.json']) {
try {
const parsed = JSON.parse(srcbook['tsconfig.json']);
metadata['tsconfig.json'] = parsed;
} catch (e) {
// This should never happen
console.error('Failed to parse tsconfig.json:', e);
}
}

return `<!-- srcbook:${JSON.stringify(metadata)} -->`;
}

function encodeTitleCell(cell: TitleCellType) {
return `# ${cell.text}`;
}
Expand Down

0 comments on commit d07230f

Please sign in to comment.