-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Output deprecation warning when use dollar prefix in global directive #183
Merged
+148
−48
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3561b1c
Output deprecation warning when use dollar prefix
yhatt ed12b84
Merge branch 'master' into deprecate-dollar-prefix
yhatt d6aea45
Update deprecation message
yhatt 09a3828
Update test of custom global directive
yhatt 7e08775
Allow aliasing from dollar-prefixed custom directive
yhatt 2bbf026
Refactor test
yhatt ac2fdd5
Initialize objects for custom directive without prototype
yhatt 9fa7676
Fix JSDoc of options and customDirectives member
yhatt 072b8e0
Update document of directives
yhatt 2c2b7bd
Update message of deprecation warning
yhatt 45f5547
[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
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 |
---|---|---|
|
@@ -4,6 +4,14 @@ import yaml from './yaml' | |
import * as directives from './directives' | ||
import marpitPlugin from '../marpit_plugin' | ||
|
||
const isComment = token => | ||
token.type === 'marpit_comment' && token.meta.marpitParsedDirectives | ||
|
||
const markAsParsed = token => { | ||
token.meta = token.meta || {} | ||
token.meta.marpitCommentParsed = 'directive' | ||
} | ||
|
||
/** | ||
* Parse Marpit directives and store result to the slide token meta. | ||
* | ||
|
@@ -20,6 +28,20 @@ import marpitPlugin from '../marpit_plugin' | |
function parse(md, opts = {}) { | ||
const { marpit } = md | ||
|
||
const applyBuiltinDirectives = (newProps, builtinDirectives) => { | ||
let ret = {} | ||
|
||
for (const prop of Object.keys(newProps)) { | ||
if (builtinDirectives[prop]) { | ||
ret = { ...ret, ...builtinDirectives[prop](newProps[prop], marpit) } | ||
} else { | ||
ret[prop] = newProps[prop] | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// Front-matter support | ||
const frontMatter = opts.frontMatter === undefined ? true : !!opts.frontMatter | ||
let frontMatterObject = {} | ||
|
@@ -45,25 +67,6 @@ function parse(md, opts = {}) { | |
}) | ||
} | ||
|
||
const isComment = token => | ||
token.type === 'marpit_comment' && token.meta.marpitParsedDirectives | ||
|
||
const markAsParsed = token => { | ||
token.meta = token.meta || {} | ||
token.meta.marpitCommentParsed = 'directive' | ||
} | ||
|
||
const filterBuiltinDirective = newProps => { | ||
const ret = {} | ||
|
||
for (const prop of Object.keys(newProps).filter( | ||
p => !directives.default.includes(p) | ||
)) | ||
ret[prop] = newProps[prop] | ||
|
||
return ret | ||
} | ||
|
||
// Parse global directives | ||
md.core.ruler.after('inline', 'marpit_directives_global_parse', state => { | ||
if (state.inlineMode) return | ||
|
@@ -73,7 +76,18 @@ function parse(md, opts = {}) { | |
let recognized = false | ||
|
||
for (const key of Object.keys(obj)) { | ||
const globalKey = key.startsWith('$') ? key.slice(1) : key | ||
const globalKey = key.startsWith('$') | ||
? (() => { | ||
if (marpit.customDirectives.global[key]) return key | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use an original directive key without warning if the dollar-prefixed custom global directive was defined. |
||
|
||
console.warn( | ||
`Deprecation warning: Dollar prefix support for global directive "${key}" is deprecated and will remove soon. Just remove "$" from "${key}" to fix ("${key.slice( | ||
1 | ||
)}").` | ||
) | ||
return key.slice(1) | ||
})() | ||
: key | ||
|
||
if (directives.globals[globalKey]) { | ||
recognized = true | ||
|
@@ -85,8 +99,9 @@ function parse(md, opts = {}) { | |
recognized = true | ||
globalDirectives = { | ||
...globalDirectives, | ||
...filterBuiltinDirective( | ||
marpit.customDirectives.global[globalKey](obj[key], marpit) | ||
...applyBuiltinDirectives( | ||
marpit.customDirectives.global[globalKey](obj[key], marpit), | ||
directives.globals | ||
), | ||
} | ||
} | ||
|
@@ -135,8 +150,9 @@ function parse(md, opts = {}) { | |
recognized = true | ||
cursor.local = { | ||
...cursor.local, | ||
...filterBuiltinDirective( | ||
marpit.customDirectives.local[key](obj[key], marpit) | ||
...applyBuiltinDirectives( | ||
marpit.customDirectives.local[key](obj[key], marpit), | ||
directives.locals | ||
), | ||
} | ||
} | ||
|
@@ -156,8 +172,9 @@ function parse(md, opts = {}) { | |
recognized = true | ||
cursor.spot = { | ||
...cursor.spot, | ||
...filterBuiltinDirective( | ||
marpit.customDirectives.local[spotKey](obj[key], marpit) | ||
...applyBuiltinDirectives( | ||
marpit.customDirectives.local[spotKey](obj[key], marpit), | ||
directives.locals | ||
), | ||
} | ||
} | ||
|
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
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.
When aliasing from custom directive to built-in directive, we will re-apply function of built-in directives to allow only valid value.