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

Follow @auto-scaling theme metadata when rendering KaTeX block #354

Merged
merged 4 commits into from
Sep 11, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Upgrade Marpit to [v2.5.2](https://github.com/marp-team/marpit/releases/v2.5.2) ([#356](https://github.com/marp-team/marp-core/pull/356))

### Fixed

- Regression: Auto-scaling for KaTeX block is always enabled regardless of `@auto-scaling` theme metadata ([#353](https://github.com/marp-team/marp-core/issues/353), [#354](https://github.com/marp-team/marp-core/pull/354))

## v3.8.0 - 2023-08-01

### Added
Expand Down
3 changes: 2 additions & 1 deletion src/math/katex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import katex from 'katex'
import { version } from 'katex/package.json'
import { isEnabledAutoScaling } from '../auto-scaling/utils'
import { getMathContext } from './context'
import katexScss from './katex.scss'

Expand Down Expand Up @@ -41,7 +42,7 @@ export const block = (marpit: any) => (tokens, idx) => {
displayMode: true,
})

if (marpit.options.inlineSVG) {
if (marpit.options.inlineSVG && isEnabledAutoScaling(marpit, 'math')) {
rendered = rendered.replace(
/^<span/i,
'<span is="marp-span" data-auto-scaling="downscale-only"',
Expand Down
50 changes: 47 additions & 3 deletions test/marp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,26 @@ function matchwo(a,b)
expect(pre.text()).toContain('CODE BLOCK')
})

it('does not add attributes for pre custom element if disabled disabled inlineSVG mode', () => {
it('does not add attributes for pre custom element if disabled inlineSVG mode', () => {
const $ = loadCheerio(marp({ inlineSVG: false }).render(markdown).html)
const pre = $('pre')

expect(pre.attr('is')).not.toBe('marp-pre')
expect(pre.is('[data-auto-scaling]')).toBe(false)
expect(pre.text()).toContain('CODE BLOCK')
})

it('does not add attributes for pre custom element if not enabled auto scaling by theme metadata', () => {
const instance = marp()
instance.themeSet.add('/* @theme test */')

const $ = loadCheerio(
instance.render(`<!-- theme: test -->\n\n${markdown}`).html,
)
const pre = $('pre')

expect(pre.attr('is')).not.toBe('marp-pre')
expect(pre.is('[data-auto-scaling]')).toBe(false)
expect(pre.text()).toContain('CODE BLOCK')
})
})
Expand All @@ -883,11 +898,26 @@ function matchwo(a,b)
expect(pre.text()).toContain('const a = 1')
})

it('does not add attributes for pre custom element if disabled disabled inlineSVG mode', () => {
it('does not add attributes for pre custom element if disabled inlineSVG mode', () => {
const $ = loadCheerio(marp({ inlineSVG: false }).render(markdown).html)
const pre = $('pre')

expect(pre.attr('is')).not.toBe('marp-pre')
expect(pre.is('[data-auto-scaling]')).toBe(false)
expect(pre.text()).toContain('const a = 1')
})

it('does not add attributes for pre custom element if not enabled auto scaling by theme metadata', () => {
const instance = marp()
instance.themeSet.add('/* @theme test */')

const $ = loadCheerio(
instance.render(`<!-- theme: test -->\n\n${markdown}`).html,
)
const pre = $('pre')

expect(pre.attr('is')).not.toBe('marp-pre')
expect(pre.is('[data-auto-scaling]')).toBe(false)
expect(pre.text()).toContain('const a = 1')
})
})
Expand All @@ -904,7 +934,7 @@ function matchwo(a,b)
expect(katex.is('[data-auto-scaling="downscale-only"]')).toBe(true)
})

it('does not add attributes for span custom element if disabled disabled inlineSVG mode', () => {
it('does not add attributes for span custom element if disabled inlineSVG mode', () => {
const $ = loadCheerio(
marp({ math: 'katex', inlineSVG: false }).render(markdown).html,
)
Expand All @@ -914,10 +944,24 @@ function matchwo(a,b)
expect(katex.is('[data-auto-scaling]')).toBe(false)
})

it('does not add attributes for span custom element if not enabled auto scaling by theme metadata', () => {
const instance = marp({ math: 'katex' })
instance.themeSet.add('/* @theme test */')

const $ = loadCheerio(
instance.render(`<!-- theme: test -->\n\n${markdown}`).html,
)
const katex = $('span.katex-display')

expect(katex.attr('is')).not.toBe('marp-span')
expect(katex.is('[data-auto-scaling]')).toBe(false)
})

describe('with MathJax', () => {
it('does not use custom element because it has already supported auto-scaling', () => {
const $ = loadCheerio(marp({ math: 'mathjax' }).render(markdown).html)
expect($('[is="marp-span"]')).toHaveLength(0)
expect($('[data-auto-scaling]')).toHaveLength(0)
})
})
})
Expand Down