Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
feat: Combine customBlocks inclusion/exclusion option
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Feb 5, 2019
1 parent 5c1dffb commit 8ae8568
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,14 @@ export interface VuePluginOptions {
[key: string]: string
}
/**
* Exclude customBlocks for final build.
* @default `['*']`
* Exclude/Include customBlocks for final build.
* @default `['!*']`
* @example
* ```js
* VuePlugin({ blackListCustomBlocks: ['markdown', 'test'] })
* VuePlugin({ customBlocks: ['markdown', '!test'] })
* ```
*/
blackListCustomBlocks?: string[]
/**
* Include customBlocks for final build.
* @default `[]`
* @example
* ```js
* VuePlugin({ blackListCustomBlocks: ['markdown', 'test'] })
* ```
*/
whiteListCustomBlocks?: string[]
customBlocks?: string[] | (() => boolean)
/**
* Inject CSS in JavaScript.
* @default `true`
Expand Down Expand Up @@ -152,24 +143,20 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
}

const shouldExtractCss = opts.css === false
const blacklisted = new Set(opts.blackListCustomBlocks || ['*'])
const whitelisted = new Set(opts.whiteListCustomBlocks || [])

const isAllowed = (customBlockType: string) =>
(!blacklisted.has('*') || !blacklisted.has(customBlockType)) &&
(whitelisted.has('*') || whitelisted.has(customBlockType))
const isAllowed = createCustomBlockFilter(opts.customBlocks)

const beforeAssemble =
opts.beforeAssemble ||
((d: DescriptorCompileResult): DescriptorCompileResult => d)

const exposeFilename =
typeof opts.exposeFilename === 'boolean' ? opts.exposeFilename : false

delete opts.beforeAssemble
delete opts.css
delete opts.exposeFilename
delete opts.blackListCustomBlocks
delete opts.whiteListCustomBlocks
delete opts.customBlocks
delete opts.defaultLang
delete opts.include
delete opts.exclude
Expand Down Expand Up @@ -382,3 +369,19 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
}
}
}

function createCustomBlockFilter(
customBlocks?: string[] | ((tag: string) => boolean)
): (tag: string) => boolean {
if (typeof customBlocks === 'function') return customBlocks
if (!Array.isArray(customBlocks)) return () => false

const allowed = new Set(customBlocks.filter(tag => !tag.startsWith('!')))
const notAllowed = new Set(
customBlocks.filter(tag => tag.startsWith('!')).map(tag => tag.substr(1))
)

return tag =>
(allowed.has('*') || allowed.has(tag)) &&
!(notAllowed.has('*') || notAllowed.has(tag))
}

0 comments on commit 8ae8568

Please sign in to comment.