Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip UTF-8 BOM from Markdown #372

Merged
merged 3 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- `--pdf-notes` option to add presenter notes into PDF as annotations ([#261](https://github.com/marp-team/marp-cli/issues/261), [#369](https://github.com/marp-team/marp-cli/pull/369))
- `author` and `keywords` metadata options / global directives ([#367](https://github.com/marp-team/marp-cli/issues/367), [#370](https://github.com/marp-team/marp-cli/pull/370))

### Fixed

- Cannot parse front-matter if input file had UTF-8 BOM ([#357](https://github.com/marp-team/marp-cli/issues/357), [#372](https://github.com/marp-team/marp-cli/pull/372))

### Changed

- Upgrade dependent packages to the latest version ([#371](https://github.com/marp-team/marp-cli/pull/371))
Expand Down
4 changes: 3 additions & 1 deletion src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export interface ConvertResult {

export type ConvertedCallback = (result: ConvertResult) => void

const stripBOM = (s: string) => (s.charCodeAt(0) === 0xfeff ? s.slice(1) : s)

export class Converter {
readonly options: ConverterOption

Expand Down Expand Up @@ -149,7 +151,7 @@ export class Converter {
: undefined,
renderer: (tplOpts) => {
const engine = this.generateEngine(tplOpts)
const ret = engine.render(`${markdown}${additionals}`)
const ret = engine.render(stripBOM(`${markdown}${additionals}`))
const info = engine[engineInfo]

if (isFile(file))
Expand Down
8 changes: 8 additions & 0 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ describe('Converter', () => {
expect(disabled.html).toContain('<i>Hello!</i>')
})

it('strips UTF-8 BOM', async () => {
const noBOM = await instance().convert('---\ntitle: test\n---')
const BOM = await instance().convert('\ufeff---\ntitle: test\n---')

expect(BOM.result).toStrictEqual(noBOM.result)
expect(BOM.rendered.title).toBe('test')
})

describe('with globalDirectives option', () => {
it('overrides theme directive', async () => {
const { rendered } = await instance({
Expand Down