oe / truncate-html Goto Github PK
View Code? Open in Web Editor NEWtruncate html and keep tag in safe
License: MIT License
truncate html and keep tag in safe
License: MIT License
NodeJS app. npm install --save truncate-html
Works fine when working locally and in development. But as soon as I do "npm run build" it fails.
node_modules/truncate-html/dist/truncate.d.ts:52:21 - error TS2304: Cannot find name 'CheerioStatic'.
52 (html: string | CheerioStatic, length?: number, options?: IOptions): string;
~~~~~~~~~~~~~
node_modules/truncate-html/dist/truncate.d.ts:53:21 - error TS2304: Cannot find name 'CheerioStatic'.
53 (html: string | CheerioStatic, options?: IOptions): string;
~~~~~~~~~~~~~
truncate('<p>some text here</p>', 3, { stripTags: true })
>> '<p>som...</p>'
Hi,
It'd be pretty nice to have this feature just like django's truncatewords_html feature.
When the truncation happens just prior a tag boundary, no ellipses are added.
Good:
> truncate('Hello <b>world</b>', 5);
'Hello...'
> truncate('Hello <b>world</b>', 7)
'Hello <b>w...</b>'
Bad:
> truncate('Hello <b>world</b>', 6)
'Hello '
When truncating by character count, it would be useful to have an option so that if the text is cut in the middle of a word it either add or removes the whole word, rather than cutting in the middle.
When I import trucate-html
using:
import * as truncate from "truncate-html";
Then I'm not able to call truncate("<p>my-html</p>")
because TypeScript compiler complains that:
Cannot invoke an expression whose type lacks a call signature.
I'm also not able to call truncate.setup()
.
When I import trucate-html
using:
import truncate from "truncate-html";
Then TypeScript compiler doesn't complain, but there are runtime errors after compilation because truncate
is undefined when imported this way.
The current version of cheerio you are using is 0.19.0. In the index.js file of cheerio for 0.19.0, line 11 reads:
exports.version = require('./package').version;
This does not work for webpack builds because it doesn't have .json
at the end of package. In 0.22.0 you will see that this has been fixed:
https://github.com/cheeriojs/cheerio/blob/0.22.0/index.js
This would be a big help if you could add this as it would allow it to be used with webpack builds.
Used this with a NativeScript project. Caused a strange error relating to "module events not found in tns_modules". Took me some time to figure out that this module was the culprit. Removed it from my project, all OK.
I just imported the latest version of the package and used truncate once in my component. I got that error when building.
Rollup: Missing Export: node_modules/truncate-html/dist/truncate.es.js:5:9
'load' is not exported by node_modules/cheerio/index.js
L4: */
L5: import { load } from 'cheerio';
While I was debugging, I spotted a small typo. ๐
https://github.com/oe/truncate-html/blob/master/dist/truncate.es.js#L2
https://github.com/oe/truncate-html/blob/master/dist/truncate.cjs.js#L2
The source of the mistake is here: https://github.com/oe/truncate-html/blob/master/rollup.config.ts#L12
I'm already doing some processing using cheerio, and I'd like to truncate the result. Currently, truncate-html only accepts text and always invokes cheerio.load
, so that's an extra HTML parse. Would be nice if I could truncate an existing cheerio $
.
There is a issue when you got text like <p>some text</p><p>Hello World</p>
you will get output: some textHello World
,
As you can see without space between text and hello
Ofc. easy fix is to regex add space after closing tag if there is no space, but it would be nice it this can do it for me.
$
is not yet assigned when html is object
line 54-59 of truncate.js
if (typeof html === 'object') {
html = $(html).html();
}
$ = cheerio.load("<div>" + html + "</div>", {
decodeEntities: options.decodeEntities
});
This happens to me when my html is actually null, so perhaps there should be a check somewhere for null as well as assigning the $
var further up
css-what <5.0.1
Severity: high
Denial of Service - https://npmjs.com/advisories/1754
fix available via npm audit fix --force
Will install [email protected], which is a breaking change
node_modules/cheerio/node_modules/css-what
css-select <=3.1.2
Depends on vulnerable versions of css-what
node_modules/cheerio/node_modules/css-select
cheerio 0.19.0 - 1.0.0-rc.3
Depends on vulnerable versions of css-select
node_modules/cheerio
truncate-html >=0.0.2
Depends on vulnerable versions of cheerio
node_modules/truncate-html
Hey,
on lines 100
and 101
(91
and 92
in .coffee
) you forgot console.log statements :)
console.log($1 + ' ~~~ ' + $2);
console.log($1.length + ' ~~~ ' + $2.length);
Using these options { length: 10, reserveLastWord: -1, ellipsis: '' }.
<p>a b c d ef</p>
gets incorrectly truncated to <p>a b c d</p>
whereas <p>a b c d ef gh</p>
is correctly truncated to <p>a b c d ef</p>
When the text is just a single word and we want to preserve it, it allows up to the character limit + 10 right now. Is there a way to make it just the character limit? The options I used to reproduce this is { length: limitLength, reserveLastWord: -1, ellipsis: '' }
. The -1
is used in the general case that I want to cut off the last word over the limit.
According to a few node vulnerability websites (snyk.io and nodesecurity.io), this package has a few vulnerabilities caused by the package nth-check. Is there any reason those haven't been updated yet?
Thanks in advance
It breaks pre
blocks, by putting everything on a single line.
Would be nice to have this optional, at least. Solved by removing the replace
call on line 84:
text = $(this).text().replace(/\s+/g, ' ');
Should I open a PR with it?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.