Skip to content

Commit

Permalink
💬 Preserve quotes in inline expressions upon request (#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 authored Jan 24, 2025
1 parent 7c5c58b commit 3b4a5f8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
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

0 comments on commit 3b4a5f8

Please sign in to comment.