-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Update readme and tests with a bunch of additional examples - Functions can return a promise - Can handle nodes with multiple content entries
- Loading branch information
Jeff Escalante
committed
Jul 5, 2016
1 parent
5627a52
commit f68f3e3
Showing
14 changed files
with
373 additions
and
74 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.DS_Store | ||
.nyc_output | ||
coverage | ||
node_modules |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
src | ||
test | ||
contributing.md | ||
.editorconfig | ||
.travis.yml | ||
.gitignore | ||
.editorconfig | ||
.github | ||
.nyc_output | ||
test | ||
coverage |
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 |
---|---|---|
@@ -1,37 +1,36 @@ | ||
const W = require('when') | ||
const intersectKeys = require('./intersectKeys') | ||
|
||
function walk (options, nodes) { | ||
nodes.map((node) => { | ||
if (typeof node !== 'object') return | ||
return W.all(nodes.map((node) => { | ||
// We're only looking for tags here | ||
if (typeof node !== 'object') return node | ||
|
||
// Also only looking for tags with attributes | ||
if (node.attrs) { | ||
// This function cross-checks to see if any of the attribute keys match | ||
// any of the keys in the user config. | ||
const opt = intersectKeys(options, node.attrs)[0] | ||
|
||
if (opt) { | ||
// If it does, remove the attribute from the tag, it's just a marker | ||
delete node.attrs[opt] | ||
node.content = [options[opt](node.content[0])] | ||
// Now for each entry in the node's content, we run the user-provided | ||
// function, which can be a promise or not. We take the results and | ||
// assign them back to the node, then return the modified node | ||
return W.map(node.content, options[opt]) | ||
.then((res) => { node.content = res; return node }) | ||
} | ||
} | ||
walk(options, node.content) | ||
}) | ||
} | ||
|
||
// helper: because it's very fast and doesnt need a dependency | ||
function intersectKeys (_a, _b) { | ||
let ai = 0 | ||
let bi = 0 | ||
let a = Object.keys(_a) | ||
let b = Object.keys(_b) | ||
const result = [] | ||
|
||
while (ai < a.length && bi < b.length) { | ||
if (a[ai] < b[bi]) { | ||
ai++ | ||
} else if (a[ai] > b[bi]) { | ||
bi++ | ||
} else { | ||
result.push(a[ai]) | ||
ai++ | ||
bi++ | ||
// If we don't match any attribute keys, return the node as usual | ||
return node | ||
} | ||
} | ||
return result | ||
|
||
// Now we recurse through the nested content | ||
return walk(options, node.content) | ||
})) | ||
} | ||
|
||
module.exports = (options) => walk.bind(null, options) | ||
module.exports = function PostHTMLContent (options) { | ||
return walk.bind(null, options) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = function intersectKeys (_a, _b) { | ||
let ai = 0 | ||
let bi = 0 | ||
let a = Object.keys(_a) | ||
let b = Object.keys(_b) | ||
const result = [] | ||
|
||
while (ai < a.length && bi < b.length) { | ||
if (a[ai] < b[bi]) { | ||
ai++ | ||
} else if (a[ai] > b[bi]) { | ||
bi++ | ||
} else { | ||
result.push(a[ai]) | ||
ai++ | ||
bi++ | ||
} | ||
} | ||
return result | ||
} |
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 |
---|---|---|
@@ -1,31 +1,43 @@ | ||
{ | ||
"name": "posthtml-content", | ||
"description": "transform contents in any way you want", | ||
"description": "Flexible content transform for posthtml", | ||
"version": "0.0.0", | ||
"author": "static-dev", | ||
"ava": { | ||
"verbose": "true" | ||
}, | ||
"bugs": "https://github.com/static-dev/posthtml-content/issues", | ||
"dependencies": { | ||
"when": "^3.7.7" | ||
}, | ||
"devDependencies": { | ||
"ava": "0.15.x", | ||
"babel-core": "^6.10.1", | ||
"babel-preset-es2015": "^6.9.0", | ||
"coveralls": "2.x", | ||
"markdown-it": "^7.0.0", | ||
"nyc": "6.x", | ||
"postcss": "^5.0.21", | ||
"postcss-nested": "^1.0.0", | ||
"posthtml": "^0.8.7", | ||
"standard": "7.x" | ||
}, | ||
"engines": { | ||
"node": ">= 6.0.0" | ||
"node": ">=6" | ||
}, | ||
"homepage": "https://github.com/static-dev/posthtml-content", | ||
"keywords": [ | ||
"html", | ||
"posthtml", | ||
"content", | ||
"posthtmlplugin" | ||
], | ||
"license": "MIT", | ||
"main": "lib", | ||
"repository": "static-dev/posthtml-content", | ||
"scripts": { | ||
"test": "ava", | ||
"lint": "standard", | ||
"coverage": "nyc ava", | ||
"coveralls": "nyc --reporter=lcov ava && cat ./coverage/lcov.info | coveralls" | ||
"coveralls": "nyc --reporter=lcov ava && cat ./coverage/lcov.info | coveralls", | ||
"pretest": "standard", | ||
"test": "nyc ava" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Hello there!</title> | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<title>Hello there!</title> | ||
</head> | ||
<body> | ||
<h1 md>**Markdown**</h1> | ||
<article md>Markdown is an **easy** to *learn* and *write* language. If you want to learn more about it checkout the following link: [Markdown](https://github.com/markdown-it/markdown-it)</article> | ||
</body> | ||
</html> |
Oops, something went wrong.