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

Reflect lang global directive to HTML templates #558

Merged
merged 5 commits into from
Oct 28, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
### Changed

- Upgrade Marpit to [v2.6.1](https://github.com/marp-team/marpit/releases/tag/v2.6.1) ([#557](https://github.com/marp-team/marp-cli/pull/557))
- Added `lang` global directive
- Upgrade Marp Core to [v3.9.0](https://github.com/marp-team/marp-core/releases/v3.9.0) ([#557](https://github.com/marp-team/marp-cli/pull/557))
- Enabled [CSS container query](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_container_queries) support for child elements of `section` element by default
- Upgrade dependent packages to the latest versions ([#557](https://github.com/marp-team/marp-cli/pull/557))
- Reflect the language defined in `lang` global directive to `<html>` element ([#542](https://github.com/marp-team/marp-cli/issues/542), [#558](https://github.com/marp-team/marp-cli/pull/558))

### Fixed

Expand Down
7 changes: 3 additions & 4 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class Converter {
fallbackToPrintableTemplate = false,
}: { fallbackToPrintableTemplate?: boolean } = {}
): Promise<TemplateResult> {
const { lang, globalDirectives, type } = this.options
const { globalDirectives, type } = this.options

const isFile = (f: File | undefined): f is File =>
!!f && f.type === FileType.File
Expand All @@ -159,7 +159,6 @@ export class Converter {

return await template({
...(this.options.templateOption || {}),
lang,
base: await resolveBase(file),
notifyWS:
isFile(file) && this.options.watch && type === ConvertType.html
Expand Down Expand Up @@ -489,8 +488,8 @@ export class Converter {
private async generateEngine(
mergeOptions: MarpitOptions
): Promise<Marpit & { [engineInfo]?: EngineInfo }> {
const { html, options } = this.options
const opts = { ...options, ...mergeOptions, html }
const { html, lang, options } = this.options
const opts = { lang, ...options, ...mergeOptions, html }

let engine = this.options.engine

Expand Down
2 changes: 2 additions & 0 deletions src/engine/info-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface EngineInfo {
description: string | undefined
image: string | undefined
keywords: string[] | undefined
lang: string | undefined
length: number
size: { height: number; width: number }
theme: string | undefined
Expand All @@ -28,6 +29,7 @@ export default function infoPlugin(md: any) {
description: globalDirectives.marpCLIDescription,
image: globalDirectives.marpCLIImage,
keywords: globalDirectives.marpCLIKeywords,
lang: globalDirectives.lang || marpit.options.lang,
title: globalDirectives.marpCLITitle,
url: globalDirectives.marpCLIURL,
size: {
Expand Down
2 changes: 1 addition & 1 deletion src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ interface TemplateRendererOptions extends Options {

interface TemplateCoreOption {
base?: string
lang: string
notifyWS?: string
renderer: (
tplOpts: TemplateRendererOptions
Expand All @@ -36,6 +35,7 @@ export interface TemplateMeta {
keywords: string[] | undefined
title: string | undefined
url: string | undefined
lang: string | undefined
}

interface RenderedSize {
Expand Down
5 changes: 5 additions & 0 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ describe('Converter', () => {
expect(result).toContain('<html lang="zh">')
})

it('prefers lang specified in Markdown than lang option', async () => {
const { result } = await instance().convert('<!-- lang: fr -->')
expect(result).toContain('<html lang="fr">')
})

it("overrides html option by converter's html option", async () => {
const defaultHtml = (await instance().convert('<i><br></i>')).rendered
expect(defaultHtml.html).toContain('&lt;i&gt;<br />&lt;/i&gt;')
Expand Down