Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💬 Preserve quotes in inline expressions upon request #1812

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-spoons-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-cli": patch
---

Add support for `strip-quotes` metadata
25 changes: 23 additions & 2 deletions packages/myst-cli/src/transforms/inlineExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { transformRenderInlineExpressions } from './inlineExpressions';
import { VFile } from 'vfile';
import type { InlineExpression } from 'myst-spec-ext';

describe('fileFromRelativePath', () => {
it('inline text is not quoted', async () => {
describe('transformRenderInlineExpressions', () => {
it('inline text is not quoted by default', async () => {
const vfile = new VFile();
const expr = {
type: 'inlineExpression',
Expand All @@ -23,4 +23,25 @@ describe('fileFromRelativePath', () => {
// Children are added and quotes are removed
expect(expr.children).toEqual([{ type: 'text', value: 'hello there' }]);
});
it('inline text is quoted when requested', async () => {
const vfile = new VFile();
const expr = {
type: 'inlineExpression',
value: '"hello " + "there"',
result: {
status: 'ok',
data: {
// Note the wrapping quotes!
'text/plain': "'hello there'",
},
metadata: {
'strip-quotes': false,
},
},
} as InlineExpression;
const tree = { type: 'root', children: [expr] };
transformRenderInlineExpressions(tree, vfile);
// Children are added and quotes are preserved
expect(expr.children).toEqual([{ type: 'text', value: "'hello there'" }]);
});
});
11 changes: 9 additions & 2 deletions packages/myst-cli/src/transforms/inlineExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function processLatex(value: string) {
.trim();
}

function processPlainText(content: string) {
function stripTextQuotes(content: string) {
return content.replace(/^(["'])(.*)\1$/, '$2');
}

Expand All @@ -57,7 +57,14 @@ function renderExpression(node: InlineExpression, file: VFile): StaticPhrasingCo
} else if (mimeType === 'text/html') {
content = [{ type: 'html', value: value as string }];
} else if (mimeType === 'text/plain') {
content = [{ type: 'text', value: processPlainText(value as string) }];
// Allow the user / libraries to explicitly indicate that quotes should be preserved
const stripQuotes = result.metadata?.['strip-quotes'] ?? true;
content = [
{
type: 'text',
value: stripQuotes ? stripTextQuotes(value as string) : (value as string),
},
];
}
});
if (content) return content;
Expand Down
Loading