Skip to content

Commit

Permalink
assert no image attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmca-glasgow committed Apr 26, 2022
1 parent e804b72 commit 8efc750
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions compiler/src/build-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { htmlPhase } from './html';
import { knitr } from './knitr/knitr';
import { texToAliasDirective } from './latex/tex-to-directive';
import { createReport, reportErrors } from './linter';
import { assertNoImageAttributes } from './linter/assert-no-image-attributes';
import { mdastPhase } from './mdast';
import { combinedMdastPhase } from './mdast/combined';
import { convertToPdf } from './pdf';
Expand Down Expand Up @@ -78,6 +79,7 @@ export async function buildUnit(unit: Unit, ctx: Context) {
}

async function inSituTransforms(file: VFile, ctx: Context) {
assertNoImageAttributes(file);
preParsePhase(file);
texToAliasDirective(file, ctx);
return mdastPhase(file, ctx);
Expand Down
26 changes: 26 additions & 0 deletions compiler/src/linter/assert-no-image-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { VFile } from 'vfile';

import { warnMessage } from '../utils/message';

export function assertNoImageAttributes(file: VFile) {
const md = file.value as string;
md.split('\n').forEach((line, idx) => {
const match = line.match(/!\[.*\]\(.*\)({.+})/);
if (match !== null) {
warnMessage(
file,
`image attributes are not supported: ${match[1]}`,
{
start: {
line: idx + 1,
column: 0,
},
end: {
line: idx + 1,
column: line.length,
},
}
);
}
});
}
12 changes: 11 additions & 1 deletion compiler/src/mdast/__test__/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {

// <figure class="img-wrapper" id="#my-alt-text">
// <div class="img-bg">
// <img src="/var/folders/dy/tzrrqxmx7_701rcnq9dnvfvh0000gp/T" alt="My alt text">
// <img src="" alt="My alt text">
// </div>
// <figcaption>
// <a href="#my-alt-text">
Expand Down Expand Up @@ -35,4 +35,14 @@ describe('images', () => {
/<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>/
);
});

it('should render an html figure with custom attributes', async () => {
const { hasWarningMessage } = await testProcessor(`
![My alt text](){width=50%}
`);

expect(
hasWarningMessage('image attributes are not supported: {width=50%}')
).toBe(true);
});
});

0 comments on commit 8efc750

Please sign in to comment.