Split posts by word and add a marker of 1/ at the end

This commit is contained in:
Rogerio Chaves 2020-05-03 00:17:27 +02:00
parent e0f602ade7
commit 60f5a40596
No known key found for this signature in database
GPG Key ID: E6AF5440509B1D94
1 changed files with 26 additions and 8 deletions

View File

@ -1,9 +1,27 @@
<% posts.map(post => { %> <% posts.map(post => {
<% if (!post.content.text) return %> if (!post.content.text) return;
<% let text = escapeMarkdown(post.content.text) %> let text = escapeMarkdown(post.content.text);
<% let limit = typeof dont_cut == "undefined" ? 140 : 10000 %> let limit = typeof dont_cut == "undefined" ? 140 : 10000;
<% let numPosts = Math.ceil(text.length / limit) %> let words = text.split(" ");
<% for (let i = numPosts; i > 0; i--) { %>
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 => { %>
<div class="post"> <div class="post">
<div> <div>
<a href="<%= profileUrl(post.author) %>"> <a href="<%= profileUrl(post.author) %>">
@ -14,8 +32,8 @@
<a href="<%= profileUrl(post.author) %>" class="no-link-style"> <a href="<%= profileUrl(post.author) %>" class="no-link-style">
<b><%= post.authorProfile.name %></b> <b><%= post.authorProfile.name %></b>
</a> </a>
<%- htmlify(text.slice(limit * (i - 1), limit * i)) %> <%- htmlify(text.replace(/^\s+/, "")) %>
</div> </div>
</div> </div>
<% } %> <% }) %>
<% }) %> <% }) %>