-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Inject browser script to rendered Markdown automatically #115
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b5653fa
Improve browser script
yhatt 658ae5e
Add Marp script plugin to inject browser script
yhatt 2891fec
Update Marp script plugin to inject to the last slide
29249f4
Fix existed failing tests
yhatt 005c8f2
Add script option to Marp constructor
yhatt 7af96d9
Deprecate Marp.ready() and provide browser.js
yhatt 4237263
Remove marpBrowser key from package.json
yhatt 19a3240
Merge branch 'master' into inject-browser-script
yhatt 18431d8
Add JSDoc for deprecated Marp.ready()
yhatt 9568d13
[ci skip] Update README.md
yhatt 2df8709
[ci skip] Update README.md
yhatt 0ebc858
Merge branch 'master' into inject-browser-script
yhatt 021571a
[ci skip] Update CHANGELOG.md
yhatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './types/src/browser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
import browser from './src/browser' | ||
|
||
browser() | ||
module.exports = require('./lib/browser.cjs') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import browser from '../src/browser' | ||
|
||
browser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
import { polyfills } from '@marp-team/marpit-svg-polyfill' | ||
import fittingObserver from './fitting/observer' | ||
import observer from './observer' | ||
|
||
export default function browser(observe = true): void { | ||
const observer = () => { | ||
for (const polyfill of polyfills()) polyfill() | ||
fittingObserver() | ||
export { observer } | ||
|
||
if (observe) window.requestAnimationFrame(observer) | ||
export default function browser(): void { | ||
if (typeof window === 'undefined') { | ||
throw new Error( | ||
"Marp Core's browser script is valid only in browser context." | ||
) | ||
} | ||
|
||
if (window['marpCoreBrowserScript']) { | ||
console.warn("Marp Core's browser script has already executed.") | ||
return | ||
} | ||
|
||
Object.defineProperty(window, 'marpCoreBrowserScript', { value: true }) | ||
observer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { polyfills } from '@marp-team/marpit-svg-polyfill' | ||
import fittingObserver from './fitting/observer' | ||
|
||
// Observer is divided for usage in Marp Web. | ||
export default function observer(keep = true): void { | ||
for (const polyfill of polyfills()) polyfill() | ||
fittingObserver() | ||
|
||
if (keep) window.requestAnimationFrame(() => observer(keep)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const dummy = 'This is a placeholder for the content of built browser script.' | ||
|
||
export default dummy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { name, version } from '../../package.json' | ||
import browserScript from './browser-script' | ||
import Marp from '../marp' | ||
|
||
interface ScriptOptionsInternal { | ||
nonce?: string | ||
source: 'inline' | 'cdn' | ||
} | ||
|
||
export interface ScriptOptions extends Partial<ScriptOptionsInternal> {} | ||
|
||
const defaultOptions: ScriptOptionsInternal = { source: 'inline' } | ||
|
||
export function markdown(md): void { | ||
const marp: Marp = md.marpit | ||
const opts = (() => { | ||
if (marp.options.script === false) return false | ||
if (marp.options.script === true) return defaultOptions | ||
|
||
return { ...defaultOptions, ...marp.options.script } | ||
})() | ||
|
||
md.core.ruler.before('marpit_collect', 'marp_core_script', state => { | ||
if (opts === false) return | ||
|
||
const lastSlideCloseIdxRev = [...state.tokens] | ||
.reverse() | ||
.findIndex(t => t.type === 'marpit_slide_close') | ||
|
||
if (lastSlideCloseIdxRev < 0) return | ||
|
||
// Inject script token to the last page | ||
const { Token } = state | ||
const scriptToken = new Token('marp_core_script', 'script', 0) | ||
|
||
scriptToken.block = true | ||
scriptToken.nesting = 0 | ||
|
||
if (opts.source === 'inline') { | ||
scriptToken.content = browserScript | ||
} else if (opts.source === 'cdn') { | ||
scriptToken.attrSet( | ||
'src', | ||
`https://cdn.jsdelivr.net/npm/${name}@${version}/lib/browser.js` | ||
) | ||
|
||
// defer attribute would have no effect in inline script | ||
scriptToken.attrSet('defer', '') | ||
} | ||
|
||
if (opts.nonce) scriptToken.attrSet('nonce', opts.nonce) | ||
|
||
state.tokens.splice(-lastSlideCloseIdxRev - 1, 0, scriptToken) | ||
}) | ||
|
||
md.renderer.rules.marp_core_script = (tokens, idx, _, __, self) => { | ||
const token = tokens[idx] | ||
return `<script${self.renderAttrs(token)}>${token.content || ''}</script>` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using a strange trick for bundling precompiled IIFE script as string by rollup, without any errors.
https://github.com/marp-team/marp-core/pull/115/files#diff-ff6e5f22a9c7e66987b19c0199636480R23