Skip to content

Commit

Permalink
Merge pull request #168 from marp-team/split-background-size
Browse files Browse the repository at this point in the history
Add size argument for split background
  • Loading branch information
yhatt authored Jun 3, 2019
2 parents 3e5161f + 7febbb9 commit 19235e2
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Allow passing markdown-it instance as `markdown` constructor option ([#164](https://github.com/marp-team/marpit/pull/164))
- Add size argument for split background ([#166](https://github.com/marp-team/marpit/issues/166), [#168](https://github.com/marp-team/marpit/pull/168))

### Fixed

Expand Down
Binary file added docs/assets/image-syntax/split-background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/image-syntax/split-bg-with-size.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/image-syntax/split-multiple-bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 42 additions & 5 deletions docs/image-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,66 @@ You may change alignment direction from horizontal to vertical, by using `vertic

The `left` or `right` keyword with `bg` keyword make a space for the background to the specified side. It has a half of slide size, and the space of a slide content will shrink too.

<div class="example">

```markdown
![bg left](https://example.com/backgroundA.jpg)
![bg left](https://picsum.photos/720?image=29)

# Split backgrounds

The space of a slide content will shrink to the right side.
```

---
<span class="image">

[<img src="/assets/image-syntax/split-background.jpg" alt="Split backgrounds" />](/assets/image-syntax/split-background.jpg ':ignore')

</span>
</div>

Multiple backgrounds will work well in the specified background side.

<!-- Multiple backgrounds will work well in the specified background side. -->
<div class="example">

![bg right](https://example.com/backgroundB.jpg)
![bg](https://example.com/backgroundC.jpg)
```markdown
![bg right](https://picsum.photos/720?image=3)
![bg](https://picsum.photos/720?image=20)

# Split + Multiple BGs

The space of a slide content will shrink to the left side.
```

<span class="image">

[<img src="/assets/image-syntax/split-multiple-bg.jpg" alt="Split + Multiple BGs" />](/assets/image-syntax/split-multiple-bg.jpg ':ignore')

</span>
</div>

This feature is similar to [Deckset's Split Slides](https://docs.decksetapp.com/English.lproj/Media/01-background-images.html#split-slides).

?> Marpit uses a last defined keyword in a slide when `left` and `right` keyword is mixed in the same slide by using multiple backgrounds.

#### Split size

Since v1.1.0, Marpit can specify split size for background by percentage like `left:33%`.

<div class="example">

```markdown
![bg left:33%](https://picsum.photos/720?image=27)

# Split backgrounds with specified size
```

<span class="image">

[<img src="/assets/image-syntax/split-bg-with-size.jpg" alt="Split backgrounds with specified size" />](/assets/image-syntax/split-bg-with-size.jpg ':ignore')

</span>
</div>

## Shorthand for setting colors

Through Markdown image syntax, Marpit allows the definition of [color value](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) instead of the image URL.
Expand Down
1 change: 1 addition & 0 deletions docsify/_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ $important-color: complement($brand-primary);
width: 100%;
max-width: 240px;
margin: 0 auto;
box-shadow: 0 3px 6px rgba(#000, 0.15);
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/markdown/background_image/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ function advancedBackground(md) {
const splitSide = t.meta.marpitBackground.split
if (splitSide) {
open.attrSet('data-marpit-advanced-background-split', splitSide)
t.attrSet('width', '50%')

if (splitSide === 'left') t.attrSet('x', '50%')
const splitBgSize = t.meta.marpitBackground.splitSize || '50%'

t.attrSet(
'width',
`${100 - Number.parseFloat(splitBgSize.slice(0, -1))}%`
)

if (splitSide === 'left') t.attrSet('x', splitBgSize)

const style = new InlineStyle(open.attrGet('style'))
style.set('--marpit-advanced-background-split', splitBgSize)
open.attrSet('style', style.toString())
}

// Add the isolated layer for background image
Expand Down
3 changes: 3 additions & 0 deletions src/markdown/background_image/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function backgroundImageApply(md) {
images: current.images,
open: current.open,
split: current.split,
splitSize: current.splitSize,
width: current.svgContent.attrGet('width'),
},
}
Expand Down Expand Up @@ -66,6 +67,7 @@ function backgroundImageApply(md) {
backgroundDirection,
backgroundSize,
backgroundSplit,
backgroundSplitSize,
color,
filter,
height,
Expand Down Expand Up @@ -105,6 +107,7 @@ function backgroundImageApply(md) {

if (backgroundDirection) current.direction = backgroundDirection
if (backgroundSplit) current.split = backgroundSplit
if (backgroundSplitSize) current.splitSize = backgroundSplitSize
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/markdown/background_image/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const bgSizeKeywords = {
fit: 'contain',
}

const splitSizeMatcher = /^(left|right)(?::((?:\d*\.)?\d+%))?$/

/**
* Marpit background image parse plugin.
*
Expand Down Expand Up @@ -38,8 +40,13 @@ function backgroundImageParse(md) {
marpitImage.backgroundSize = bgSizeKeywords[opt]

// Split background keyword
if (opt === 'left' || opt === 'right')
marpitImage.backgroundSplit = opt
const matched = opt.match(splitSizeMatcher)
if (matched) {
const [, splitSide, splitSize] = matched

marpitImage.backgroundSplit = splitSide
marpitImage.backgroundSplitSize = splitSize
}

// Background aligned direction
if (opt === 'vertical' || opt === 'horizontal')
Expand Down
4 changes: 2 additions & 2 deletions src/postcss/advanced_background.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ section[data-marpit-advanced-background="background"] > div[data-marpit-advanced
}
section[data-marpit-advanced-background="background"][data-marpit-advanced-background-split] > div[data-marpit-advanced-background-container] {
width: 50%;
width: var(--marpit-advanced-background-split, 50%);
}
section[data-marpit-advanced-background="background"][data-marpit-advanced-background-split="right"] > div[data-marpit-advanced-background-container] {
margin-left: 50%;
margin-left: calc(100% - var(--marpit-advanced-background-split, 50%));
}
section[data-marpit-advanced-background="background"] > div[data-marpit-advanced-background-container] > figure {
Expand Down
30 changes: 28 additions & 2 deletions test/markdown/background_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,45 @@ describe('Marpit background image plugin', () => {

splitBackgroundKeywords.forEach(keyword => {
context(`with the ${keyword} keyword for split background`, () => {
const $ = $load(mdSVG().render(`![bg ${keyword}](image)`))
const section = $(
const $ = attr =>
$load(
mdSVG().render(
`![bg ${[keyword, attr].filter(v => v).join(':')}](image)`
)
)

const section = $()(
'svg > foreignObject > section[data-marpit-advanced-background="content"]'
)
const foreignObject = section.parent()

it('assigns the width attribute of foreignObject for content as 50%', () =>
expect(foreignObject.attr('width')).toBe('50%'))

it('assigns CSS variable to style attribute for split background', () =>
expect(section.attr('style')).toContain(
'--marpit-advanced-background-split:50%'
))

it('assigns data attribute of the keyword for split background', () =>
expect(section.attr('data-marpit-advanced-background-split')).toBe(
keyword
))

context('when passed split size for background', () => {
const ssSection = $('33%')(
'svg > foreignObject > section[data-marpit-advanced-background="content"]'
)
const ssforeignObject = ssSection.parent()

it('assigns the width attribute of foreignObject for content as remaining size', () =>
expect(ssforeignObject.attr('width')).toBe('67%'))

it('assigns CSS variable to style attribute for split background', () =>
expect(ssSection.attr('style')).toContain(
'--marpit-advanced-background-split:33%'
))
})
})
})

Expand Down

0 comments on commit 19235e2

Please sign in to comment.