From 60f5a4059614643c2d7dd2feff97e8d59c72c7d6 Mon Sep 17 00:00:00 2001 From: Rogerio Chaves Date: Sun, 3 May 2020 00:17:27 +0200 Subject: [PATCH] Split posts by word and add a marker of 1/ at the end --- web/views/shared/_posts.ejs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/web/views/shared/_posts.ejs b/web/views/shared/_posts.ejs index 38a0e9e..5affab4 100644 --- a/web/views/shared/_posts.ejs +++ b/web/views/shared/_posts.ejs @@ -1,9 +1,27 @@ -<% posts.map(post => { %> - <% if (!post.content.text) return %> - <% let text = escapeMarkdown(post.content.text) %> - <% let limit = typeof dont_cut == "undefined" ? 140 : 10000 %> - <% let numPosts = Math.ceil(text.length / limit) %> - <% for (let i = numPosts; i > 0; i--) { %> +<% posts.map(post => { + if (!post.content.text) return; + let text = escapeMarkdown(post.content.text); + let limit = typeof dont_cut == "undefined" ? 140 : 10000; + let words = text.split(" "); + + let splittedPosts = []; + let nextPost = ""; + for (word of words) { + const postsCount = splittedPosts.length + 1; + const pageMarker = `${postsCount}/`; + + if (nextPost.length + word.length + pageMarker.length + 1 < limit) { + nextPost += word + " "; + } else { + splittedPosts.push(nextPost + pageMarker); + nextPost = word + " "; + } + } + const postsCount = splittedPosts.length + 1; + const lastMarker = postsCount > 1 ? `${postsCount}/${postsCount}` : ""; + splittedPosts.push(nextPost + lastMarker); + + splittedPosts.reverse().map(text => { %>
@@ -14,8 +32,8 @@ <%= post.authorProfile.name %> - <%- htmlify(text.slice(limit * (i - 1), limit * i)) %> + <%- htmlify(text.replace(/^\s+/, "")) %>
- <% } %> + <% }) %> <% }) %> \ No newline at end of file