Skip to content

Commit 32f11e4

Browse files
committed
.
0 parents  commit 32f11e4

12 files changed

+534
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.github/workflows/bb.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: bb
2+
on:
3+
issues:
4+
types: [opened, reopened, edited, closed, labeled, unlabeled]
5+
pull_request_target:
6+
types: [opened, reopened, edited, closed, labeled, unlabeled]
7+
jobs:
8+
main:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: unifiedjs/beep-boop-beta@main
12+
with:
13+
repo-token: ${{secrets.GITHUB_TOKEN}}

.github/workflows/main.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: main
2+
on:
3+
- pull_request
4+
- push
5+
jobs:
6+
main:
7+
name: ${{matrix.node}}
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: dcodeIO/setup-node-nvm@master
12+
with:
13+
node-version: ${{matrix.node}}
14+
- run: npm install
15+
- run: npm test
16+
- uses: codecov/codecov-action@v1
17+
strategy:
18+
matrix:
19+
node:
20+
- lts/erbium
21+
- node

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.d.ts
3+
*.log
4+
coverage/
5+
node_modules/
6+
yarn.lock

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage/
2+
*.md

index.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @typedef {import('hast').Root} Root
3+
* @typedef {import('hast').Content} Content
4+
* @typedef {import('hast').Text} Text
5+
* @typedef {Root|Content} Node
6+
*
7+
* @typedef Options
8+
* Configuration.
9+
* @property {string} [comment='more']
10+
* Comment value to search for.
11+
* @property {number} [maxSearchSize=2048]
12+
* How far to search for the comment before bailing.
13+
* The goal of this project is to find user-defined explicit excerpts, that
14+
* are assumed to be somewhat reasonably placed.
15+
* This option prevents searching giant documents for some comment
16+
* that probably won’t be found at the end.
17+
* @property {Content[]} [ignore=[]]
18+
* Nodes to exclude from the resulting tree.
19+
* These are not counted towards `size`.
20+
*/
21+
22+
import {truncate} from 'hast-util-truncate'
23+
24+
/**
25+
* Truncate the tree to a certain comment.
26+
* Returns a copy of the given tree if a comment is found, and `undefined`
27+
* otherwise.
28+
*
29+
* @template {Node} Tree
30+
* @param {Tree} tree
31+
* @param {Options} [options]
32+
* @returns {Tree|undefined}
33+
*/
34+
export function excerpt(tree, options = {}) {
35+
const {comment = 'more', ignore, maxSearchSize = 2048} = options
36+
let found = false
37+
const result = preorder(truncate(tree, {ignore, size: maxSearchSize}))
38+
39+
// @ts-expect-error: `result` is most likely a clone of `tree`
40+
return found ? result : undefined
41+
42+
/**
43+
* @param {Node} node
44+
* @returns {Node|undefined}
45+
*/
46+
function preorder(node) {
47+
if (node.type === 'comment' && node.value.trim() === comment) {
48+
found = true
49+
return
50+
}
51+
52+
if (
53+
// @ts-expect-error: integrate w/ `mdast-util-mdx`
54+
(node.type === 'mdxFlowExpression' ||
55+
// @ts-expect-error
56+
node.type === 'mdxTextExpression') &&
57+
// @ts-expect-error
58+
node.data &&
59+
// @ts-expect-error
60+
node.data.estree &&
61+
// @ts-expect-error
62+
node.data.estree.comments &&
63+
// @ts-expect-error
64+
node.data.estree.comments.some(
65+
(/** @type {import('acorn').Comment} */ node) =>
66+
node.value.trim() === comment
67+
)
68+
) {
69+
found = true
70+
return
71+
}
72+
73+
/** @type {typeof node} */
74+
const replacement = {...node}
75+
76+
if ('children' in node) {
77+
/** @type {Content[]} */
78+
const children = []
79+
let index = -1
80+
81+
while (++index < node.children.length) {
82+
const result = preorder(node.children[index])
83+
84+
if (found) {
85+
break
86+
}
87+
88+
// @ts-expect-error: assume content model matches.
89+
if (result) children.push(result)
90+
}
91+
92+
// @ts-expect-error: assume content model matches.
93+
replacement.children = children
94+
}
95+
96+
return replacement
97+
}
98+
}

license

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2021 Titus Wormer <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"name": "hast-util-excerpt",
3+
"version": "0.0.0",
4+
"description": "hast utility to excerpt the tree to a certain number of characters",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"hast",
9+
"hast-util",
10+
"util",
11+
"utility",
12+
"html",
13+
"excerpt",
14+
"excerpt",
15+
"summary"
16+
],
17+
"repository": "syntax-tree/hast-util-excerpt",
18+
"bugs": "https://github.com/syntax-tree/hast-util-excerpt/issues",
19+
"funding": {
20+
"type": "opencollective",
21+
"url": "https://opencollective.com/unified"
22+
},
23+
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
24+
"contributors": [
25+
"Titus Wormer <[email protected]> (https://wooorm.com)"
26+
],
27+
"sideEffects": false,
28+
"type": "module",
29+
"main": "index.js",
30+
"types": "index.d.ts",
31+
"files": [
32+
"index.d.ts",
33+
"index.js"
34+
],
35+
"dependencies": {
36+
"@types/hast": "^2.0.0",
37+
"hast-util-truncate": "^1.0.0"
38+
},
39+
"devDependencies": {
40+
"@types/acorn": "^4.0.6",
41+
"@types/tape": "^4.0.0",
42+
"c8": "^7.0.0",
43+
"hast-util-select": "^5.0.0",
44+
"hastscript": "^7.0.0",
45+
"mdast-util-from-markdown": "^1.0.0",
46+
"mdast-util-mdx": "^1.0.0",
47+
"mdast-util-to-hast": "^11.0.0",
48+
"micromark-extension-mdxjs": "^1.0.0",
49+
"prettier": "^2.0.0",
50+
"remark-cli": "^10.0.0",
51+
"remark-preset-wooorm": "^9.0.0",
52+
"rimraf": "^3.0.0",
53+
"tape": "^5.0.0",
54+
"type-coverage": "^2.0.0",
55+
"typescript": "^4.0.0",
56+
"unist-builder": "^3.0.0",
57+
"unist-util-remove-position": "^4.0.0",
58+
"xo": "^0.44.0"
59+
},
60+
"scripts": {
61+
"prepack": "npm run build && npm run format",
62+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
63+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
64+
"test-api": "node test.js",
65+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
66+
"test": "npm run build && npm run format && npm run test-coverage"
67+
},
68+
"prettier": {
69+
"tabWidth": 2,
70+
"useTabs": false,
71+
"singleQuote": true,
72+
"bracketSpacing": false,
73+
"semi": false,
74+
"trailingComma": "none"
75+
},
76+
"xo": {
77+
"prettier": true
78+
},
79+
"remarkConfig": {
80+
"plugins": [
81+
"preset-wooorm"
82+
]
83+
},
84+
"typeCoverage": {
85+
"atLeast": 100,
86+
"detail": true,
87+
"strict": true,
88+
"ignoreCatch": true
89+
}
90+
}

0 commit comments

Comments
 (0)