Giter Site home page Giter Site logo

downsize's People

Contributors

adam-zethraeus avatar aiampogi avatar cgiffard avatar halfdan avatar remy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

downsize's Issues

Trailing empty tags occur.

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>"

Truncation Breaks if HTML Comment Contains Quotes

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;
                     }

downsize seems does not handle Asian languages

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.

create a system to count by blocks

status quo:

  • downsize("", {words: #});
  • downsize("", {characters: #});

suggested addition:

  • downsize("", {blocks: 5, blockTypes:{"p","pre","blockquote","ul","li"}});

Intended as a generic version of paragraphs:5 or something, but that could be added as a good shortcut too.

What do you think?

Entities and smart quotes cause erroneous results

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&rsquo;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!

system to maintain initial image

What do you think would be a good API to preserve an initial image in the HTML?

downsize("", {words:0, keepInitialImage:true});

downsize doesn't handle invalid html

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})

Option to remove scripts and embeds

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?

Add travis

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)

Issue Summary

Tags with single quotes inside double quotes doesn't get parsed/truncated correctly according to the limit (by words/characters)

Steps to Reproduce

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.

Technical details

  • Client OS: Windows 8
  • Server OS: Windows 8
  • Node Version: v0.10.24
  • Browser: Chrome

directly related to: TryGhost/Ghost#2106

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.