Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function doGet(e) {
- var docId = e && e.parameter && e.parameter.id ? e.parameter.id : 'YOUR_DEFAULT_DOC_ID';
- var page = e && e.parameter && e.parameter.page ? parseInt(e.parameter.page) : 1;
- var postsPerPage = 5;
- var doc = DocumentApp.openById(docId);
- var paragraphs = doc.getBody().getParagraphs();
- var allPosts = parseContentRich(paragraphs);
- var paginatedPosts = paginatePosts(allPosts, page, postsPerPage);
- var jsonOutput = JSON.stringify({
- documentTitle: doc.getName(),
- posts: paginatedPosts.posts,
- totalPosts: allPosts.length,
- currentPage: page,
- hasMore: paginatedPosts.hasMore
- });
- return ContentService.createTextOutput(jsonOutput)
- .setMimeType(ContentService.MimeType.JSON);
- }
- function parseContentRich(paragraphs) {
- var posts = [];
- var currentPost = null;
- for (var i = 0; i < paragraphs.length; i++) {
- var para = paragraphs[i];
- var plainLine = para.getText().trim();
- var htmlLine = textElementToHtml(para);
- if (plainLine.startsWith('---POST---')) {
- if (currentPost) {
- posts.push(currentPost);
- }
- currentPost = {
- title: plainLine.replace('---POST---', '').trim(),
- publishDate: '',
- content: []
- };
- } else if (plainLine.startsWith('---DATE---') && currentPost) {
- currentPost.publishDate = plainLine.replace('---DATE---', '').trim();
- } else if (currentPost) {
- currentPost.content.push(htmlLine);
- }
- }
- if (currentPost) {
- posts.push(currentPost);
- }
- // Join content lines with <br>, or use <p> if you want real paragraphs
- return posts.map(function(post) {
- return {
- title: post.title,
- publishDate: post.publishDate,
- content: post.content.join('<br>\n').trim()
- };
- });
- }
- // Converts a Paragraph to HTML with formatting
- function textElementToHtml(para) {
- var html = '';
- for (var j = 0; j < para.getNumChildren(); j++) {
- var elem = para.getChild(j);
- if (elem.getType() === DocumentApp.ElementType.TEXT) {
- html += processTextElem(elem);
- }
- }
- return html;
- }
- function processTextElem(textElem) {
- var text = textElem.getText();
- var html = '';
- var i = 0;
- while (i < text.length) {
- // Get the formatting at the current position
- var bold = textElem.isBold(i);
- var italic = textElem.isItalic(i);
- var underline = textElem.isUnderline(i);
- var url = textElem.getLinkUrl(i);
- // Find the next point where formatting changes
- var j = i + 1;
- while (j < text.length &&
- textElem.isBold(j) === bold &&
- textElem.isItalic(j) === italic &&
- textElem.isUnderline(j) === underline &&
- textElem.getLinkUrl(j) === url) {
- j++;
- }
- // Get the substring with the same formatting
- var substr = text.substring(i, j)
- .replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
- // Wrap the substring as needed
- if (url) substr = '<a href="' + url + '" target="_blank">' + substr + '</a>';
- if (underline) substr = '<u>' + substr + '</u>';
- if (italic) substr = '<em>' + substr + '</em>';
- if (bold) substr = '<strong>' + substr + '</strong>';
- html += substr;
- i = j;
- }
- return html;
- }
- function paginatePosts(posts, page, postsPerPage) {
- var startIndex = (page - 1) * postsPerPage;
- var endIndex = startIndex + postsPerPage;
- var paginatedPosts = posts.slice(startIndex, endIndex);
- return {
- posts: paginatedPosts,
- hasMore: endIndex < posts.length
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment