From e5e43d949df4ca2fa8611d9ea31e1bf4e9a8021d Mon Sep 17 00:00:00 2001 From: Dave Star Date: Fri, 29 Apr 2022 15:04:33 +0100 Subject: [PATCH] fix tests --- compiler/src/cli/cli.ts | 5 ++ compiler/src/context.ts | 1 + compiler/src/mdast/__test__/images.test.ts | 55 +++++++++++++--------- compiler/src/mdast/embed-asset-url.ts | 14 ++++-- compiler/src/mdast/index.ts | 4 +- compiler/src/test-utils/test-processor.ts | 1 + 6 files changed, 51 insertions(+), 29 deletions(-) diff --git a/compiler/src/cli/cli.ts b/compiler/src/cli/cli.ts index 482aaac1..becf7d39 100644 --- a/compiler/src/cli/cli.ts +++ b/compiler/src/cli/cli.ts @@ -40,6 +40,10 @@ const { argv } = yargs(process.argv.slice(2)) type: 'boolean', description: "Don't embed assets", }) + .option('noEmbedAssetUrl', { + type: 'boolean', + description: "Don't complete asset Url", + }) .option('noCache', { type: 'boolean', description: 'No cache', @@ -72,6 +76,7 @@ const options: Options = { noSyntaxHighlight: argv.noSyntaxHighlight, noReport: argv.noReport, noEmbedAssets: argv.noEmbedAssets, + noEmbedAssetUrl: argv.noEmbedAssetUrl, noCache: argv.noCache, noTexSvg: argv.noTexSvg, spelling: argv.spelling, diff --git a/compiler/src/context.ts b/compiler/src/context.ts index a1baabd0..e2b7d8d2 100644 --- a/compiler/src/context.ts +++ b/compiler/src/context.ts @@ -10,6 +10,7 @@ export type Options = { noReport?: boolean; reportOnlyErrors?: boolean; noEmbedAssets?: boolean; + noEmbedAssetUrl?: boolean; noCache?: boolean; noTexSvg?: boolean; week?: number; diff --git a/compiler/src/mdast/__test__/images.test.ts b/compiler/src/mdast/__test__/images.test.ts index 13880048..cf30cb57 100644 --- a/compiler/src/mdast/__test__/images.test.ts +++ b/compiler/src/mdast/__test__/images.test.ts @@ -3,37 +3,48 @@ import { testProcessor, } from '../../test-utils/test-processor'; -//
-//
-// My alt text -//
-//
-// -// Figure 1: -// My alt text -// -//
-//
- describe('images', () => { it('should render an html figure', async () => { - const { html } = await testProcessor(` - ![]() + const { html } = await testProcessor('![]()'); + + const expected = ignoreWhitespace(` +

+
+
+ +
+
+ + Figure 1 + +
+
+

`); - expect(ignoreWhitespace(html)).toMatch( - /<\/div>
Figure1<\/span><\/a><\/figcaption><\/figure>/ - ); + expect(ignoreWhitespace(html)).toBe(expected); }); it('should render an html figure with alt text', async () => { - const { html } = await testProcessor(` - ![My alt text]() + const { html } = await testProcessor('![My alt text]()'); + + const expected = ignoreWhitespace(` +

+
+
+ My alt text +
+
+ + Figure 1: + My alt text + +
+
+

`); - expect(ignoreWhitespace(html)).toMatch( - /<\/div>
Figure1:<\/span>Myalttext<\/a><\/figcaption><\/figure>/ - ); + expect(ignoreWhitespace(html)).toBe(expected); }); it('should render an html figure with custom attributes', async () => { diff --git a/compiler/src/mdast/embed-asset-url.ts b/compiler/src/mdast/embed-asset-url.ts index 94e61a58..0c73de57 100644 --- a/compiler/src/mdast/embed-asset-url.ts +++ b/compiler/src/mdast/embed-asset-url.ts @@ -3,7 +3,9 @@ import path from 'path'; import { Literal, Root } from 'mdast'; import { visit } from 'unist-util-visit'; -export function embedAssetUrl() { +import { Context } from '../context'; + +export function embedAssetUrl(ctx: Context) { return async (tree: Root) => { let activeDir = ''; @@ -19,7 +21,7 @@ export function embedAssetUrl() { } if (node.type === 'image') { - node.url = getPath(node.url, activeDir); + node.url = getPath(node.url, activeDir, ctx); } // also fix for raw html nodes sometimes output by knitr @@ -29,7 +31,7 @@ export function embedAssetUrl() { const { src, ...otherProps } = props; Object.assign(node, { type: 'image', - url: getPath(src, activeDir), + url: getPath(src, activeDir, ctx), value: '', data: otherProps, }); @@ -39,8 +41,10 @@ export function embedAssetUrl() { }; } -function getPath(url: string, dirname: string) { - return path.isAbsolute(url) || url.startsWith('http') +function getPath(url: string, dirname: string, ctx: Context) { + return path.isAbsolute(url) || + url.startsWith('http') || + ctx.options.noEmbedAssetUrl ? url : path.join(dirname, url); } diff --git a/compiler/src/mdast/index.ts b/compiler/src/mdast/index.ts index 70ad554d..f4bbdeb8 100644 --- a/compiler/src/mdast/index.ts +++ b/compiler/src/mdast/index.ts @@ -38,13 +38,13 @@ export async function mdastPhase(file: VFile, ctx: Context) { linkProperties: { className: 'link' }, }) // custom plugins: + .use(columns) + .use(embedAssetUrl, ctx) .use(youtubeVideos) .use(aliasDirectiveToSvg, ctx) .use(removeEmptyParagraphs) // .use(aliasDirectiveToTex, ctx) .use(codeBlocks, ctx) - .use(columns) - .use(embedAssetUrl) .use(images, ctx) .use(pagebreaks); diff --git a/compiler/src/test-utils/test-processor.ts b/compiler/src/test-utils/test-processor.ts index ce280358..92a7fb3f 100644 --- a/compiler/src/test-utils/test-processor.ts +++ b/compiler/src/test-utils/test-processor.ts @@ -91,6 +91,7 @@ async function createTestContext(md: string, options: Options = {}) { noSyntaxHighlight: true, noReport: true, noEmbedAssets: true, + noEmbedAssetUrl: true, force: true, format: true, ...options,