-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from raghur/generate-inline-svg
Generate inline svg
- Loading branch information
Showing
6 changed files
with
208 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
const pandoc = require('pandoc-filter') | ||
/* global test jest describe expect beforeEach */ | ||
describe('mermaid', () => { | ||
let fs, exec, filter | ||
beforeEach(() => { | ||
jest.mock('fs') | ||
jest.mock('process', () => ({ | ||
env: {} | ||
})) | ||
jest.mock('tmp', () => ({ | ||
fileSync: jest.fn().mockReturnValue({ | ||
name: 'tmpfile' | ||
}) | ||
})) | ||
jest.mock('child_process', () => ({ | ||
execSync: jest.fn() | ||
})) | ||
fs = require('fs') | ||
exec = require('child_process').execSync | ||
fs.existsSync = jest.fn().mockReturnValue(true) | ||
filter = require('./filter') | ||
|
||
fs.writeFileSync = jest.fn() | ||
fs.readFileSync = jest.fn().mockReturnValue('graph TD;\nA-->B;') | ||
jest.clearAllMocks() | ||
}) | ||
test('returns null for non code block', () => { | ||
const type = 'Paragraph' | ||
const value = [] | ||
const format = '' | ||
const meta = {} | ||
|
||
expect(filter.mermaid(type, value, format, meta)).toBeNull() | ||
}) | ||
|
||
test('returns null if no mermaid class', () => { | ||
const type = 'CodeBlock' | ||
const value = [['id', ['other']], 'graph TD;\nA-->B;'] | ||
const format = '' | ||
const meta = {} | ||
|
||
expect(filter.mermaid(type, value, format, meta)).toBeNull() | ||
}) | ||
|
||
test('renders with default options', () => { | ||
filter.mermaid('CodeBlock', [['id', ['mermaid']], 'x']) | ||
|
||
expect(exec).toHaveBeenCalled() | ||
const cmd = exec.mock.lastCall[0] | ||
console.log('cmd', cmd) | ||
expect(cmd).toContain('mmdc') | ||
expect(cmd).toContain('-s 1') | ||
expect(cmd).toContain('-f') | ||
expect(cmd).toContain('-i "tmpfile"') | ||
expect(cmd).toContain('-t default') | ||
expect(cmd).toContain('-w 800') | ||
expect(cmd).toContain('-b white') | ||
expect(cmd).toContain('-o "tmpfile.png"') | ||
}) | ||
|
||
test('returns RawBlock for svg inline', () => { | ||
const block = filter.mermaid('CodeBlock', [['id', ['mermaid'], [['format', 'svg']]], 'x']) | ||
|
||
expect(block).toEqual(expect.objectContaining({ | ||
t: 'RawBlock', | ||
c: ['html', 'graph TD;\nA-->B;'] | ||
})) | ||
expect(exec).toHaveBeenCalled() | ||
const cmd = exec.mock.lastCall[0] | ||
console.log('cmd', cmd) | ||
expect(cmd).toContain('mmdc') | ||
expect(cmd).toContain('-s 1') | ||
expect(cmd).toContain('-f') | ||
expect(cmd).toContain('-i "tmpfile"') | ||
expect(cmd).toContain('-t default') | ||
expect(cmd).toContain('-w 800') | ||
expect(cmd).toContain('-b white') | ||
expect(cmd).toContain('-o "tmpfile.svg"') | ||
}) | ||
|
||
test('sets title as fig: if id starts with fig:', () => { | ||
const para = filter.mermaid('CodeBlock', | ||
[['fig:someref', | ||
['mermaid']], 'x']) | ||
|
||
expect(para).toEqual(expect.objectContaining({ | ||
t: 'Para', | ||
c: [ | ||
{ | ||
t: 'Image', | ||
c: [ | ||
['fig:someref', [], []], | ||
[pandoc.Str('')], | ||
[expect.any(String), 'fig:'] | ||
] | ||
} | ||
] | ||
})) | ||
}) | ||
|
||
test('sets alt from caption attribute', () => { | ||
const para = filter.mermaid('CodeBlock', | ||
[['id', | ||
['mermaid'], | ||
[['caption', 'a caption']]], 'x']) | ||
|
||
expect(para).toEqual(expect.objectContaining({ | ||
t: 'Para', | ||
c: [ | ||
{ | ||
t: 'Image', | ||
c: [ | ||
['id', [], []], | ||
[pandoc.Str('a caption')], | ||
[expect.any(String), ''] | ||
] | ||
} | ||
] | ||
})) | ||
|
||
expect(exec).toHaveBeenCalled() | ||
const cmd = exec.mock.lastCall[0] | ||
console.log('cmd', cmd) | ||
expect(cmd).toContain('mmdc') | ||
expect(cmd).toContain('-s 1') | ||
expect(cmd).toContain('-f') | ||
expect(cmd).toContain('-i "tmpfile"') | ||
expect(cmd).toContain('-t default') | ||
expect(cmd).toContain('-w 800') | ||
expect(cmd).toContain('-b white') | ||
expect(cmd).toContain('-o "tmpfile.png"') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
#! /bin/bash | ||
TEST_OUT=test-output | ||
mkdir -p $TEST_OUT | ||
pandoc -t json test.md > $TEST_OUT/test.json | ||
cat $TEST_OUT/test.json | node index.js > $TEST_OUT/transformed.json | ||
pandoc -f json $TEST_OUT/transformed.json -t html > $TEST_OUT/transformed.html | ||
xdg-open $TEST_OUT/transformed.html | ||
pushd $TEST_OUT | ||
pandoc -t json ../test.md > test.json | ||
cat test.json | node ../index.js > transformed.json | ||
pandoc -f json transformed.json -t html > transformed.html | ||
xdg-open transformed.html | ||
popd |