cgiffard / downsize Goto Github PK
View Code? Open in Web Editor NEWTag safe text truncation for HTML and XML!
License: BSD 3-Clause "New" or "Revised" License
Tag safe text truncation for HTML and XML!
License: BSD 3-Clause "New" or "Revised" License
Lines 27 and 37 of index.js fall into zero if falsy territory.
Trailing empty tags occur.
i.e.
it("should not have trailing empty tags", function () {
downsize("<p>there are five words here</p><i>what</i>", {words: 5})
.should.equal("<p>there are five words here</p>");
});
result:
AssertionError: expected '<p>there are five words here</p><i></i>' to equal '<p>there are five words here</p>'
+ expected - actual
+"<p>there are five words here</p>"
-"<p>there are five words here</p><i></i>"
For example:
const html = `<!-- it's broken -->one two three`;
const truncate = require('downsize');
console.log(truncate(html, {words: 2}));
Results in:
<!-- it's broken -->one two three
This fixes it:
diff --git a/index.js b/index.js
index 03cbc9b..42e14c6 100644
--- a/index.js
+++ b/index.js
@@ -42,7 +42,7 @@ var XRegexp = require('xregexp').XRegExp;
}
options.keepContext = !!options.contextualTags;
- options.contextualTags =
+ options.contextualTags =
options.keepContext && Array.isArray(options.contextualTags) ?
options.contextualTags : [];
@@ -161,7 +161,8 @@ var XRegexp = require('xregexp').XRegExp;
// if double quote is found in a single quote string,
// ignore it and let the string finish
break;
-
+ } else if (parseState === PARSER_COMMENT) {
+ break;
} else if (parseState !== PARSER_UNINITIALISED) {
parseState = PARSER_TAG_STRING;
}
@@ -176,7 +177,8 @@ var XRegexp = require('xregexp').XRegExp;
// if single quote is found in a double quote string,
// ignore it and let the string finish
break;
-
+ } else if (parseState === PARSER_COMMENT) {
+ break;
} else if (parseState !== PARSER_UNINITIALISED) {
parseState = PARSER_TAG_STRING_SINGLE;
}
bring it up to speed.
For the character based Asian languages, "word" and "character" are actually the same concept, and words are not separated.
For example the English sentence "The quick brown fox jumps over the lazy dog" in Chinese is "敏捷的棕毛狐狸从懒狗身上跃过". downsize that sentence to 2, we expect the result to be "敏捷", but the actual result(treated the whole sentence as a single word) is not.
status quo:
suggested addition:
Intended as a generic version of paragraphs:5 or something, but that could be added as a good shortcut too.
What do you think?
Not sure if this is an easy fix or not. If a word has smart quotes (either raw or entities) it will be split.
Examples:
// With smart quotes
downsize('<p>This <em>isn’t</em> right.</p>', {
words: 2
}) // outputs: <p>This <em>isn’</em></p>
// With smart quotes are HTML entities
downsize('<p>This <em>isn’t</em> right.</p>', {
words: 2
}) // outputs: <p>This <em>isn&</em></p>
I'm also curious to know if Downsize still being maintained (asking because last commit was in 2014). Thanks!
What do you think would be a good API to preserve an initial image in the HTML?
downsize("", {words:0, keepInitialImage:true});
Hi,
it's probably up for discussion if it should, but I ran into that issue in my ghost blog:
downsize returns more than the requests words if there is a img tag with a src attribute ending with "":
downsize('<p>some <img src="foo.jpg""> here</p><p>and some more here</p>', {"words": 2})
var d = require('downsize');
var html = '<p>a b c d</p>';
console.log(d(html,{words:2, append: '...'}));
Outputs:
<p>a ...</p>
Downsize is all about text truncation, we want to take some HTML or XML, and get a truncated version.
An issue was recently raised in Ghost suggesting that it doesn't make sense to include the content of <script>
tags in our {{excerpt}}
. The content which appears between <script>
tags isn't really text, so doesn't make much sense for the text-only version.
There are a set of tags for which this is true, going through https://developer.mozilla.org/en/docs/Web/Guide/HTML/HTML5/HTML5_element_list I'd suggest:
<script>
, <style>
, <template>
and all the embedded content tags: <iframe>
, <embed>
, <object>
, <param>
, <video>
, <audio>
, <source>
, <track>
, <canvas>
, <map>
, <area>
, <svg>
and <math>
.
However, I'm not sure what other use-cases there are for downsize beyond Ghost so I definitely suggest that this should be optional, but I think it makes sense to add the ability to remove these tags entirely from the truncated version?
It'd be nice to get the visibility inline. Might have a crack at it when I get home.
Tags with single quotes inside double quotes doesn't get parsed/truncated correctly according to the limit (by words/characters)
ddownsize('<p><img src="/someUrl.jpg" alt="Let\'s get in!"></p><p>hello world</p>', {words:"1"});
results to:
'<p><img src="/someUrl.jpg" alt="Let\'s get in!"></p><p>hello world</p></p>'
But it should result to:
'<p><img src="/someUrl.jpg" alt="Let's get in!"></p><p>hello</p>'
This is a bug because the specified number of html words should be followed even if the alt text of the has a single quote/apostrophe.
directly related to: TryGhost/Ghost#2106
it("should not have trailing empty tags", function () {
downsize("<p>characters</p><i>what</i>", {characters: 10})
.should.equal("<p>characters</p>");
});
This fails with an extra <i></i>
just as words mode did before https://github.com/zethraeus/Downsize/commit/5568bfe68d050e3073168daaf18da77a43cb8d9b
i have not further researched why.
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.