From e38d67602447bad97b14059d7ab7d3893268a907 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 15 Oct 2023 03:10:42 +0900 Subject: [PATCH] Prefer lang global directive than constructor option --- src/markdown/directives/apply.js | 4 ++-- src/marpit.js | 2 ++ test/markdown/directives/apply.js | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/markdown/directives/apply.js b/src/markdown/directives/apply.js index 61b3a80..1ce81cd 100644 --- a/src/markdown/directives/apply.js +++ b/src/markdown/directives/apply.js @@ -70,8 +70,8 @@ function _apply(md, opts = {}) { } // Apply attribute to token - if (lang || marpitDirectives.lang) - token.attrSet('lang', lang || marpitDirectives.lang) + if (marpitDirectives.lang || lang) + token.attrSet('lang', marpitDirectives.lang || lang) if (marpitDirectives.class) token.attrJoin('class', marpitDirectives.class) diff --git a/src/marpit.js b/src/marpit.js index 7dbe90c..14a0529 100644 --- a/src/marpit.js +++ b/src/marpit.js @@ -72,6 +72,8 @@ class Marpit { * slide page at before of headings. it would apply to headings whose * larger than or equal to the specified level if a number is given, or * ONLY specified levels if a number array. + * @param {string} [opts.lang] Set the default `lang` attribute of each slide. + * It can override by `lang` global directive in the Markdown. * @param {boolean} [opts.looseYAML=false] Allow loose YAML parsing in * built-in directives, and custom directives defined in current instance. * @param {MarkdownIt|string|Object|Array} [opts.markdown] An instance of diff --git a/test/markdown/directives/apply.js b/test/markdown/directives/apply.js index 595bc5d..92a84d6 100644 --- a/test/markdown/directives/apply.js +++ b/test/markdown/directives/apply.js @@ -120,6 +120,11 @@ describe('Marpit directives apply plugin', () => { expect(sections.eq(1).attr('lang')).toBe('en-US') }) + it('can override the language that is setting by `lang` constructor option', () => { + const $lang = load(md({ options: { lang: 'fr' } }).render(langDir)) + expect($lang('section').first().attr('lang')).toBe('en-US') + }) + context('when lang directive is not defined', () => { it('follows the lang option of Marpit instance', () => { const $ = load(md({}).render(''))