Skip to content

Commit

Permalink
Merge pull request #3 from marp-team/html-lang
Browse files Browse the repository at this point in the history
Fix incorrect CJK fonts in exported PDF
  • Loading branch information
yhatt authored Aug 24, 2018
2 parents e547247 + 1b20b75 commit 0f73d4c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Fix incorrect CJK fonts in exported PDF ([#3](https://github.com/marp-team/marp-cli/pull/3))

## v0.0.4 - 2018-08-23

### Added
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"chrome-launcher": "^0.10.2",
"globby": "^8.0.1",
"is-wsl": "^1.1.0",
"os-locale": "^3.0.0",
"puppeteer-core": "^1.7.0",
"yargs": "^12.0.1"
}
Expand Down
2 changes: 2 additions & 0 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum ConvertType {

export interface ConverterOption {
engine: typeof Marpit
lang: string
options: MarpitOptions
output?: string
readyScript?: string
Expand Down Expand Up @@ -52,6 +53,7 @@ export class Converter {
additionals += `\n<!-- theme: ${JSON.stringify(this.options.theme)} -->`

return this.template({
lang: this.options.lang,
readyScript: this.options.readyScript,
renderer: tplOpts =>
this.generateEngine(tplOpts).render(`${markdown}${additionals}`),
Expand Down
4 changes: 3 additions & 1 deletion src/marp-cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Marp } from '@marp-team/marp-core'
import { version as coreVersion } from '@marp-team/marp-core/package.json'
import path from 'path'
import globby from 'globby'
import osLocale from 'os-locale'
import path from 'path'
import { Argv } from 'yargs'
import yargs from 'yargs/yargs'
import * as cli from './cli'
Expand Down Expand Up @@ -76,6 +77,7 @@ export default async function(argv: string[] = []): Promise<number> {
// Initialize converter
const converter = new Converter({
engine: Marp,
lang: (await osLocale()).replace(/[_@]/g, '-'),
options: {},
output: args.output,
readyScript: await MarpReadyScript.bundled(),
Expand Down
1 change: 1 addition & 0 deletions src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bareScss from './templates/bare.scss'
import { MarpitOptions, MarpitRenderResult } from '@marp-team/marpit'

export interface TemplateOptions {
lang: string
readyScript?: string
renderer: (tplOpts: MarpitOptions) => MarpitRenderResult
[prop: string]: any
Expand Down
2 changes: 1 addition & 1 deletion src/templates/bare.pug
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
doctype
html(lang="en")
html(lang=lang)
head
meta(charset="UTF-8")
meta(name="viewport", content="width=device-width, initial-scale=1.0")
Expand Down
47 changes: 29 additions & 18 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Converter', () => {

const instance = (opts = {}) =>
new Converter({
lang: 'en',
engine: Marp,
options: {},
template: 'bare',
Expand All @@ -25,8 +26,9 @@ describe('Converter', () => {
describe('#constructor', () => {
it('assigns initial options to options member', () => {
const options = {
lang: 'en',
engine: Marp,
options: { html: true } as MarpitOptions,
options: <MarpitOptions>{ html: true },
template: 'test-template',
type: ConvertType.html,
}
Expand Down Expand Up @@ -76,6 +78,11 @@ describe('Converter', () => {
expect(subject).toThrow(CLIError)
})

it('settings lang attribute of <html> by lang option', () => {
const { result } = instance({ lang: 'zh' }).convert('')
expect(result).toContain('<html lang="zh">')
})

it("overrides theme by converter's theme option", () => {
const { rendered } = instance({ theme: 'gaia' }).convert('')
expect(rendered.css).toContain('@theme gaia')
Expand Down Expand Up @@ -145,23 +152,27 @@ describe('Converter', () => {
const pdfInstance = (opts = {}) =>
instance({ ...opts, type: ConvertType.pdf })

it('converts markdown file into PDF', async () => {
const write = jest.spyOn(fs, 'writeFile')

return useSpy([write], async () => {
write.mockImplementation((_, __, callback) => callback())

const opts = { output: 'test.pdf' }
const result = await pdfInstance(opts).convertFile(onePath)
const pdf: Buffer = write.mock.calls[0][1]

expect(write).toHaveBeenCalled()
expect(write.mock.calls[0][0]).toBe('test.pdf')
expect(pdf.toString('ascii', 0, 5)).toBe('%PDF-')
expect(result.output).toBe('test.pdf')
expect(result.result).toBe(write.mock.calls[0][1])
})
})
it(
'converts markdown file into PDF',
async () => {
const write = jest.spyOn(fs, 'writeFile')

return useSpy([write], async () => {
write.mockImplementation((_, __, callback) => callback())

const opts = { output: 'test.pdf' }
const result = await pdfInstance(opts).convertFile(onePath)
const pdf: Buffer = write.mock.calls[0][1]

expect(write).toHaveBeenCalled()
expect(write.mock.calls[0][0]).toBe('test.pdf')
expect(pdf.toString('ascii', 0, 5)).toBe('%PDF-')
expect(result.output).toBe('test.pdf')
expect(result.result).toBe(write.mock.calls[0][1])
})
},
10000
)
})
})

Expand Down
43 changes: 42 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ cross-spawn@^5.0.1:
shebang-command "^1.2.0"
which "^1.2.9"

cross-spawn@^6.0.4:
cross-spawn@^6.0.0, cross-spawn@^6.0.4:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
dependencies:
Expand Down Expand Up @@ -1777,6 +1777,18 @@ exec-sh@^0.2.0:
dependencies:
merge "^1.2.0"

execa@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.10.0.tgz#ff456a8f53f90f8eccc71a96d11bdfc7f082cb50"
dependencies:
cross-spawn "^6.0.0"
get-stream "^3.0.0"
is-stream "^1.1.0"
npm-run-path "^2.0.0"
p-finally "^1.0.0"
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"
Expand Down Expand Up @@ -2485,6 +2497,10 @@ invert-kv@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6"

invert-kv@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-2.0.0.tgz#7393f5afa59ec9ff5f67a27620d11c226e3eec02"

is-absolute-url@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-absolute-url/-/is-absolute-url-2.1.0.tgz#50530dfb84fcc9aa7dbe7852e83a37b93b9f2aa6"
Expand Down Expand Up @@ -3378,6 +3394,12 @@ lcid@^1.0.0:
dependencies:
invert-kv "^1.0.0"

lcid@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/lcid/-/lcid-2.0.0.tgz#6ef5d2df60e52f82eb228a4c373e8d1f397253cf"
dependencies:
invert-kv "^2.0.0"

left-pad@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
Expand Down Expand Up @@ -3620,6 +3642,13 @@ mem@^1.1.0:
dependencies:
mimic-fn "^1.0.0"

mem@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/mem/-/mem-3.0.1.tgz#152410d0d7e835e4a4363e626238d9e5be3d6f5a"
dependencies:
mimic-fn "^1.0.0"
p-is-promise "^1.1.0"

memorystream@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/memorystream/-/memorystream-0.3.1.tgz#86d7090b30ce455d63fbae12dda51a47ddcaf9b2"
Expand Down Expand Up @@ -4101,6 +4130,14 @@ os-locale@^2.0.0:
lcid "^1.0.0"
mem "^1.1.0"

os-locale@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-3.0.0.tgz#b868acc459f0f81c8ddde3a11cb0b030f8dd7bf0"
dependencies:
execa "^0.10.0"
lcid "^2.0.0"
mem "^3.0.1"

os-tmpdir@^1.0.0, os-tmpdir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
Expand All @@ -4116,6 +4153,10 @@ p-finally@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"

p-is-promise@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-1.1.0.tgz#9c9456989e9f6588017b0434d56097675c3da05e"

p-limit@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.3.0.tgz#b86bd5f0c25690911c7590fcbfc2010d54b3ccb8"
Expand Down

0 comments on commit 0f73d4c

Please sign in to comment.