https://github.com/thibaultboursier/tag2content
Replace a tag with content in a configurable way
https://github.com/thibaultboursier/tag2content
content regex replace tag template
Last synced: over 1 year ago
JSON representation
Replace a tag with content in a configurable way
- Host: GitHub
- URL: https://github.com/thibaultboursier/tag2content
- Owner: thibaultboursier
- Created: 2018-12-16T12:52:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-08T10:44:04.000Z (over 7 years ago)
- Last Synced: 2024-04-26T10:44:58.466Z (over 2 years ago)
- Topics: content, regex, replace, tag, template
- Language: TypeScript
- Size: 382 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tag2content
Replace a tag with content in a configurable way
[](https://travis-ci.com/thibaultboursier/tag2content)
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save tag2content
```
## Import
```js
// ES6
import tag2content from 'tag2content';
// CommonJS
const tag2content = require('tag2content').default;
```
## Basic usage
Create a text, in which you define some tags:
```js
const text = 'I would like to go to [country] next year, with my friend [friend-name].';
```
Create a configuration for you tags:
```js
const tags = {
country: () => 'Japan',
'friend-name': () => 'Joe',
};
```
Replace tags with their content:
```js
const updatedText = tag2content({
tags,
text,
});
console.log(updatedText) //=> 'I would like to go to Japan next year, with my friend Joe.';
```
## Variables
You can add some variables to your tags:
```js
const text = 'I go to [country name="England"] next year, with [friend name="Lucy" to-uppercase="true"].';
```
When you defines a function for each tag, variables are passed as argument. Kebab case variables are transformed to Pascal case ("to-uppercase" becomes "toUppercase").
```js
const tags = {
country: variables => {
return variables.name;
},
friend: ({ name, toUppercase }) => {
return toUppercase ? name.toUpperCase() : name;
}
};
```
## Custom delimiters
You can use custom delimiters:
```js
const text = 'My first name is {first-name value="Robin"}.';
const tags = {
'first-name': ({ value }) => value,
};
const updatedText = tag2content({
delimiters: {
end: '}',
start: '{',
},
tags,
text,
});
console.log(updatedText) //=> 'My first name is Robin.';
```