0.1.0 • Published 5 years ago
metalsmith-var-replace v0.1.0
metalsmith-one-replace
A single Metalsmith/markdown plugin to perform multiple types of replace action within markdown files. The supported replace functions are:
- Insert content from other file
- Variable substitution
- Regex replacement
The default tags (override by defining custom regular expression in keyRegex config attribute) are :
- {#insert ...} - to insert a file
- {#var ...} - for variable substitution
For detailed usage, refer to wiki.
Config
Example - Simple (to insert file using {#insert} custom tag)
...
{
actions:[{
type:'file'
}]
}
...
Example - Simple (to substitute variable using {#var} custom tag})
...
{
actions:[{
type:'var',
varValues:{
'f1':'Apple',
'f2':'Orange'
}
}]
}
...
Example - Simple (defining custom tag {#img} to use instead of within markdown files)
...
{
actions:[{
type:'replace',
fileFilter:'.md$',
replacePatterns:{
'{#img (.*?)}':'<img $1>'
}
}]
}
...
Example - Detailed
...
{
consoleLog: false,
enabled: true,
actions: [{
priority: 10,
type:'file'
},{
priority: 20,
type:'var',
varValues:{
'f1':'Apple',
'f2':'Orange',
'f3':'Banana'
}
},{
priority: 1,
type:'replace',
fileFilter:'.svg$',
replacePatterns:{
'\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>':'',
'(xml([a-z:-A-Z0-9]+))=[\"]?((?:.(?![\"]?\s+(?:\S+)=|[>\"]))+.)[\"]?([ ]+)?':'',
'^\s*$':'',
'<(([a-z]+)*[^>]+)\/>':'<$1></$2>',
'([a-z:-A-Z0-9]+)="null"?([ ]+)?':''
}
},{
enabled: false,
priority:5,
type:'replace',
fileFilter:'.md$',
replacePatterns:{
'{#img (.*?)}':'<img $1>',
'{#bold (.*?)}':'<b>$1</b>'
}
}]
}
...
More information about config attributes:
consoleLog
(optional boolean default:false
) - to log the info to consoleenabled
(optional boolean default:true
) - to enable/disable the entire pluginactions
(array of action objects) is mandatorypriority
(optional number) - to specify the sequence order while processingenabled
(optional boolean default:true
) - to enable/disable a specific actionfileFilter
(optional regex string default:(.*?)
) - to filter files for processing under a specific actiontype
(string values arevar
,file
, andreplace
) is mandatory - to specify the routine while processing- attributes when
type
isvar
keyRegex
(optional regex string default:{#var (.*?)}
) - regex to find the tag (example{#var}
) with the variable name as the parametervarValues
(key-value pair object) is mandatory - with variable name and it's value as key-value pairignoreMissing
(optional boolean default:false
) - ignore (by substituting it with blank) if the variable is not defined
- attributes when
type
isfile
keyRegex
(optional regex string default:^{#insert (.*)}
) - regex to find the tag (example{#insert}
) with the name of the file (with path) as parameterignoreMissing
(optional boolean default:false
) - ignore (by substituting it with blank) if the file is not foundremoveFile
(optional boolean default:true
) - remove the file from target folder after inserting the content
- attributes when
type
isreplace
replacePatterns
(key-value pair object) is mandatory - regex find and replace string as key-value pair
- attributes when
Documentation
Refer to the wiki
License
MIT