Skip to content

Commit

Permalink
Merge pull request #101 from UofGAnalytics/inline-assets-in-production
Browse files Browse the repository at this point in the history
ensure svgs are inlined in production build
  • Loading branch information
dmca-glasgow committed Apr 28, 2022
2 parents 82d6a6f + d140e8c commit 11005bc
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 18 deletions.
4 changes: 2 additions & 2 deletions compiler/src/hast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { unified } from 'unified';
import { VFile } from 'vfile';

import { Context } from '../context';
import { embedAssets } from './embed-assets';
import { inlineRelativeAssets } from './inline-files';
import { responsiveTables } from './responsive-tables';

export async function hastPhase(
Expand All @@ -20,7 +20,7 @@ export async function hastPhase(
.use(responsiveTables);

if (!ctx.options.noEmbedAssets) {
processor.use(embedAssets, ctx);
processor.use(inlineRelativeAssets, ctx);
}

return processor.run(mdast, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getSvgHast } from '../utils/get-svg-hast';
import { failMessage } from '../utils/message';
import { readFile, rehypeParser } from '../utils/utils';

export function embedAssets(ctx: Context) {
export function inlineRelativeAssets(ctx: Context) {
return async (tree: Element, file: VFile) => {
const transformations: Promise<void>[] = [];
visit(tree, 'element', (node) => {
Expand Down Expand Up @@ -85,11 +85,16 @@ async function embedSvg(imgNode: Element, ctx: Context) {
// const svgNode = getAssetHast(svg) as Element;

const svgNode = getSvgHast(svg);
const svgProperties = svgNode.properties || {};

// helps to ensure the svg is responsive
delete svgProperties.width;
delete svgProperties.height;

const className = 'knitr-svg';
const properties = {
...imgNode.properties,
...svgNode.properties,
...svgProperties,
className: [
className,
...getNodeClassNames(imgNode, className),
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/mdast/__test__/embed-asset-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('embedAssetUrl', () => {
});
// console.log(html);
const imgCount = (html.match(/img-wrapper/g) || []).length;
expect(imgCount).toBe(2);
expect(imgCount).toBe(3);

// const svgCount = (html.match(/<svg.*?style="width: 70%;".*?>/) || [])
// .length;
Expand All @@ -17,6 +17,6 @@ describe('embedAssetUrl', () => {
.match(/<span class="caption-count">Figure (\d+)/g)
?.map((s) => Number(s.slice(-1)));

expect(figureNum).toEqual([1, 2]);
expect(figureNum).toEqual([1, 2, 3]);
});
});
70 changes: 70 additions & 0 deletions fixtures/relative-assets/test/dag-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions fixtures/relative-assets/test/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ content:
src: test1.Rmd
- type: text
src: test2.Rmd
# - type: text
# src: test3.Rmd
- type: text
src: test3.Rmd
4 changes: 1 addition & 3 deletions fixtures/relative-assets/test/test3.Rmd
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
```{r, echo=FALSE, out.width = '70%'}
knitr::include_graphics("venn_disjoint.pdf")
```
![Dag logo](dag-logo.svg)
7 changes: 7 additions & 0 deletions template/src/styles/images.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ figure {
.img-bg {
display: inline-block;
background: white;
width: 75%;
max-width: 100%;
height: auto;
margin-top: 3rem;
margin-right: auto;
margin-bottom: 3rem;
margin-left: auto;
}
svg.pdftex, svg.knitr-svg {
margin-bottom: 0;
Expand Down
7 changes: 0 additions & 7 deletions template/src/styles/tex.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
svg.pdftex,
svg.knitr-svg {
display: block;
width: 75%;
max-width: 100%;
height: auto;
margin-top: 3rem;
margin-right: auto;
margin-bottom: 3rem;
margin-left: auto;
// background: white;
// path {
// stroke: white;
Expand Down

0 comments on commit 11005bc

Please sign in to comment.