Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmca-glasgow committed Apr 29, 2022
1 parent a2457d0 commit e5e43d9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
5 changes: 5 additions & 0 deletions compiler/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Options = {
noReport?: boolean;
reportOnlyErrors?: boolean;
noEmbedAssets?: boolean;
noEmbedAssetUrl?: boolean;
noCache?: boolean;
noTexSvg?: boolean;
week?: number;
Expand Down
55 changes: 33 additions & 22 deletions compiler/src/mdast/__test__/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,48 @@ import {
testProcessor,
} from '../../test-utils/test-processor';

// <figure class="img-wrapper" id="#my-alt-text">
// <div class="img-bg">
// <img src="" alt="My alt text">
// </div>
// <figcaption>
// <a href="#my-alt-text">
// <span class="caption-count">Figure 1: </span>
// My alt text
// </a>
// </figcaption>
// </figure>

describe('images', () => {
it('should render an html figure', async () => {
const { html } = await testProcessor(`
![]()
const { html } = await testProcessor('![]()');

const expected = ignoreWhitespace(`
<p></p>
<figure class="img-wrapper" id="figure-1">
<div class="img-bg">
<img src="" alt="">
</div>
<figcaption>
<a href="#figure-1">
<span class="caption-count">Figure 1</span>
</a>
</figcaption>
</figure>
<p></p>
`);

expect(ignoreWhitespace(html)).toMatch(
/<figureclass="img-wrapper"id="figure-1"><divclass="img-bg"><imgsrc=".+"alt=""><\/div><figcaption><ahref="#figure-1"><spanclass="caption-count">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(`
<p></p>
<figure class="img-wrapper" id="my-alt-text">
<div class="img-bg">
<img src="" alt="My alt text">
</div>
<figcaption>
<a href="#my-alt-text">
<span class="caption-count">Figure 1: </span>
My alt text
</a>
</figcaption>
</figure>
<p></p>
`);

expect(ignoreWhitespace(html)).toMatch(
/<figureclass="img-wrapper"id="my-alt-text"><divclass="img-bg"><imgsrc=".+"alt="Myalttext"><\/div><figcaption><ahref="#my-alt-text"><spanclass="caption-count">Figure1:<\/span>Myalttext<\/a><\/figcaption><\/figure>/
);
expect(ignoreWhitespace(html)).toBe(expected);
});

it('should render an html figure with custom attributes', async () => {
Expand Down
14 changes: 9 additions & 5 deletions compiler/src/mdast/embed-asset-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand All @@ -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
Expand All @@ -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,
});
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/mdast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions compiler/src/test-utils/test-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ async function createTestContext(md: string, options: Options = {}) {
noSyntaxHighlight: true,
noReport: true,
noEmbedAssets: true,
noEmbedAssetUrl: true,
force: true,
format: true,
...options,
Expand Down

0 comments on commit e5e43d9

Please sign in to comment.