Skip to content

Commit

Permalink
Add lang directive and lang constructor option
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Oct 14, 2023
1 parent fd5a6cc commit 1a7c7ee
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare namespace Marpit {
anchor?: boolean | AnchorCallback
container?: false | Element | Element[]
headingDivider?: false | HeadingDivider | HeadingDivider[]
lang?: string
looseYAML?: boolean
markdown?: any
printable?: boolean
Expand Down
4 changes: 4 additions & 0 deletions src/markdown/directives/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import builtInDirectives from './directives'
*/
function _apply(md, opts = {}) {
const { marpit } = md
const { lang } = marpit.options

const dataset = opts.dataset === undefined ? true : !!opts.dataset
const css = opts.css === undefined ? true : !!opts.css
Expand Down Expand Up @@ -69,6 +70,9 @@ function _apply(md, opts = {}) {
}

// Apply attribute to token
if (lang || marpitDirectives.lang)
token.attrSet('lang', lang || marpitDirectives.lang)

if (marpitDirectives.class)
token.attrJoin('class', marpitDirectives.class)

Expand Down
3 changes: 3 additions & 0 deletions src/markdown/directives/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @prop {Directive} headingDivider Specify heading divider option.
* @prop {Directive} style Specify the CSS style to apply additionally.
* @prop {Directive} theme Specify theme of the slide deck.
* @prop {Directive} lang Specify the language of the slide deck. It will
* assign as `lang` attribute for each slide.
*/
export const globals = Object.assign(Object.create(null), {
headingDivider: (value) => {
Expand All @@ -41,6 +43,7 @@ export const globals = Object.assign(Object.create(null), {
},
style: (v) => ({ style: v }),
theme: (v, marpit) => (marpit.themeSet.has(v) ? { theme: v } : {}),
lang: (v) => ({ lang: v }),
})

/**
Expand Down
1 change: 1 addition & 0 deletions src/marpit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const defaultOptions = {
anchor: true,
container: marpitContainer,
headingDivider: false,
lang: undefined,
looseYAML: false,
markdown: undefined,
printable: true,
Expand Down
30 changes: 30 additions & 0 deletions test/markdown/directives/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@ describe('Marpit directives apply plugin', () => {
})
})

describe('Global directives', () => {
describe('lang directive', () => {
const langDir = dedent`
---
lang: en-US
---
---
`

it('applies lang attribute to each section element', () => {
const $ = load(mdForTest().render(langDir))
const sections = $('section')

expect(sections.eq(0).attr('lang')).toBe('en-US')
expect(sections.eq(1).attr('lang')).toBe('en-US')
})

context('when lang directive is not defined', () => {
it('follows the lang option of Marpit instance', () => {
const $ = load(md({}).render(''))
expect($('section').first().attr('lang')).toBeUndefined()

const $lang = load(md({ options: { lang: 'en-US' } }).render(''))
expect($lang('section').first().attr('lang')).toBe('en-US')
})
})
})
})

describe('Local directives', () => {
describe('Background image', () => {
const bgDirs = dedent`
Expand Down

0 comments on commit 1a7c7ee

Please sign in to comment.