Skip to content

Commit

Permalink
Merge pull request #75 from posthtml/feat/esm
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin authored Feb 28, 2024
2 parents 3e8f16f + 15e2c92 commit e686d25
Show file tree
Hide file tree
Showing 13 changed files with 10,607 additions and 18,009 deletions.
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2023
},
"plugins": ["@typescript-eslint"],
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
"quotes": [2, "single"],
"linebreak-style": [2, "unix"],
"camelcase": [2, {"properties": "always"}],
"brace-style": [2, "1tbs", {"allowSingleLine": true}]
},
"env": {
"es6": true,
"node": true,
"browser": false
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
]
}
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ updates:
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [14, 16, 18, 20]
node-version: [18, 20]

steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 0 additions & 5 deletions .npmignore

This file was deleted.

21 changes: 16 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
License (MIT)
-------------
MIT License (MIT)

Copyright (c) 2016 Jeff Escalante, Carrot Creative

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
## Install

```
npm i posthtml-content
npm i posthtml posthtml-content
```

## Usage
Expand All @@ -34,11 +34,11 @@ import posthtml from'posthtml'
import content from'posthtml-content'

const html = posthtml([
content({
// Map your custom attribute to a function that takes and returns a string
uppercase: str => str.toUpperCase()
})
])
content({
// Map your custom attribute to a function that takes and returns a string
uppercase: str => str.toUpperCase()
})
])
.process('<p uppercase>posthtml is great</p>')
.then(result => result.html)
```
Expand All @@ -62,10 +62,10 @@ import posthtml from'posthtml'
import content from'posthtml-content'

const html = posthtml([
content({
append: (content, attrValue) => content + attrValue
})
])
content({
append: (content, attrValue) => content + attrValue
})
])
.process('<p append=" bar">foo</p>')
.then(result => result.html)
```
Expand Down Expand Up @@ -222,15 +222,11 @@ const {html} = await posthtml([plugin]).process(html)

`posthtml-content` is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

## Contributing

See [contributing.md](contributing.md) for details on running tests and contributing.

[npm]: https://www.npmjs.com/package/posthtml-content
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-content.svg
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-content
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-content.svg
[github-ci]: https://github.com/posthtml/posthtml-content/actions/workflows/nodejs.yml
[github-ci-shield]: https://github.com/posthtml/posthtml-content/actions/workflows/nodejs.yml/badge.svg
[license]: ./license
[license]: ./LICENSE
[license-shield]: https://img.shields.io/npm/l/posthtml-content.svg
30 changes: 0 additions & 30 deletions contributing.md

This file was deleted.

53 changes: 29 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const intersectKeys = require('./intersect-keys.js');
import intersectKeys from './intersect-keys.js'

function lowercaseEntities(string) {
return String(string).replace(/&[A-Z]+;/g, match => match.toLowerCase());
return String(string).replace(/&[A-Z]+;/g, match => match.toLowerCase())
}

function processContent(options, content, attribute, attributeValue) {
// If content is an array, recursively process each item
if (Array.isArray(content)) {
return Promise.all(content.map(childContent => processContent(options, childContent, attribute, attributeValue)));
return Promise.all(content.map(childContent => processContent(options, childContent, attribute, attributeValue)))
}

if (typeof content === 'object' && content !== null) {
Expand All @@ -16,46 +16,48 @@ function processContent(options, content, attribute, attributeValue) {
return Promise.resolve(processContent(options, content.content, attribute, attributeValue))
.then(updatedContent => {
// Update the object's content property after promises are resolved
content.content = updatedContent;
return content;
});
content.content = updatedContent

return content
})
}

// Return content unchanged
return Promise.resolve(content);
return Promise.resolve(content)
}

if (typeof options[attribute] === 'function') {
// If content is a string and options[attribute] is a function, apply the function
// Lowercase HTML entities in case they were uppercased
return Promise
.resolve(options[attribute](String(content), attributeValue))
.then(content => lowercaseEntities(content));
.then(content => lowercaseEntities(content))
}

// If options[attribute] is not a function, return the content unchanged
return Promise.resolve(content);
return Promise.resolve(content)
}

function walk(options, node) {
// Only look for tags with attributes
if (node.attrs) {
// Get the first attribute name matching any of the keys in the user config
const opt = intersectKeys(options, node.attrs)[0];
const opt = intersectKeys(options, node.attrs)[0]

if (opt !== undefined) {
const attribute = opt;
const attributeValue = node.attrs[attribute];
const attribute = opt
const attributeValue = node.attrs[attribute]
// If it does, remove the attribute from the tag, it's just a marker
delete node.attrs[attribute];
delete node.attrs[attribute]

// Now process the content based on its type (string, array of nodes, or object)
return processContent(options, node.content, attribute, attributeValue)
.then(result => {
// Update the node content after promises are resolved
node.content = result;
return node;
});
node.content = result

return node
})
}
}

Expand All @@ -64,19 +66,22 @@ function walk(options, node) {
return Promise.all(node.content.map(childNode => walk(options, childNode)))
.then(updatedContent => {
// Update the node content after promises are resolved
node.content = updatedContent;
return node;
});
node.content = updatedContent

return node
})
}

return Promise.resolve(node);
return Promise.resolve(node)
}

// Options passed in by the user
module.exports = options => {
const plugin = options => {
// Nodes passed from PostHTML
return nodes => {
return Promise.all(nodes.map(node => walk(options, node)))
.then(updatedNodes => updatedNodes);
};
};
.then(updatedNodes => updatedNodes)
}
}

export default plugin
4 changes: 3 additions & 1 deletion lib/intersect-keys.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (_a, _b) => {
const intersectKeys = (_a, _b) => {
let ai = 0
let bi = 0
const result = []
Expand All @@ -19,3 +19,5 @@ module.exports = (_a, _b) => {

return result
}

export default intersectKeys
Loading

0 comments on commit e686d25

Please sign in to comment.