Guest User

Untitled

a guest
Aug 1st, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. function doGet(e) {
  2. var docId = e && e.parameter && e.parameter.id ? e.parameter.id : 'YOUR_DEFAULT_DOC_ID';
  3. var page = e && e.parameter && e.parameter.page ? parseInt(e.parameter.page) : 1;
  4. var postsPerPage = 5;
  5.  
  6. var doc = DocumentApp.openById(docId);
  7. var paragraphs = doc.getBody().getParagraphs();
  8. var allPosts = parseContentRich(paragraphs);
  9. var paginatedPosts = paginatePosts(allPosts, page, postsPerPage);
  10.  
  11. var jsonOutput = JSON.stringify({
  12. documentTitle: doc.getName(),
  13. posts: paginatedPosts.posts,
  14. totalPosts: allPosts.length,
  15. currentPage: page,
  16. hasMore: paginatedPosts.hasMore
  17. });
  18.  
  19. return ContentService.createTextOutput(jsonOutput)
  20. .setMimeType(ContentService.MimeType.JSON);
  21. }
  22.  
  23. function parseContentRich(paragraphs) {
  24. var posts = [];
  25. var currentPost = null;
  26.  
  27. for (var i = 0; i < paragraphs.length; i++) {
  28. var para = paragraphs[i];
  29. var plainLine = para.getText().trim();
  30. var htmlLine = textElementToHtml(para);
  31.  
  32. if (plainLine.startsWith('---POST---')) {
  33. if (currentPost) {
  34. posts.push(currentPost);
  35. }
  36. currentPost = {
  37. title: plainLine.replace('---POST---', '').trim(),
  38. publishDate: '',
  39. content: []
  40. };
  41. } else if (plainLine.startsWith('---DATE---') && currentPost) {
  42. currentPost.publishDate = plainLine.replace('---DATE---', '').trim();
  43. } else if (currentPost) {
  44. currentPost.content.push(htmlLine);
  45. }
  46. }
  47. if (currentPost) {
  48. posts.push(currentPost);
  49. }
  50.  
  51. // Join content lines with <br>, or use <p> if you want real paragraphs
  52. return posts.map(function(post) {
  53. return {
  54. title: post.title,
  55. publishDate: post.publishDate,
  56. content: post.content.join('<br>\n').trim()
  57. };
  58. });
  59. }
  60.  
  61. // Converts a Paragraph to HTML with formatting
  62. function textElementToHtml(para) {
  63. var html = '';
  64. for (var j = 0; j < para.getNumChildren(); j++) {
  65. var elem = para.getChild(j);
  66. if (elem.getType() === DocumentApp.ElementType.TEXT) {
  67. html += processTextElem(elem);
  68. }
  69. }
  70. return html;
  71. }
  72.  
  73. function processTextElem(textElem) {
  74. var text = textElem.getText();
  75. var html = '';
  76. var i = 0;
  77. while (i < text.length) {
  78. // Get the formatting at the current position
  79. var bold = textElem.isBold(i);
  80. var italic = textElem.isItalic(i);
  81. var underline = textElem.isUnderline(i);
  82. var url = textElem.getLinkUrl(i);
  83.  
  84. // Find the next point where formatting changes
  85. var j = i + 1;
  86. while (j < text.length &&
  87. textElem.isBold(j) === bold &&
  88. textElem.isItalic(j) === italic &&
  89. textElem.isUnderline(j) === underline &&
  90. textElem.getLinkUrl(j) === url) {
  91. j++;
  92. }
  93.  
  94. // Get the substring with the same formatting
  95. var substr = text.substring(i, j)
  96. .replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
  97.  
  98. // Wrap the substring as needed
  99. if (url) substr = '<a href="' + url + '" target="_blank">' + substr + '</a>';
  100. if (underline) substr = '<u>' + substr + '</u>';
  101. if (italic) substr = '<em>' + substr + '</em>';
  102. if (bold) substr = '<strong>' + substr + '</strong>';
  103.  
  104. html += substr;
  105. i = j;
  106. }
  107. return html;
  108. }
  109.  
  110. function paginatePosts(posts, page, postsPerPage) {
  111. var startIndex = (page - 1) * postsPerPage;
  112. var endIndex = startIndex + postsPerPage;
  113. var paginatedPosts = posts.slice(startIndex, endIndex);
  114.  
  115. return {
  116. posts: paginatedPosts,
  117. hasMore: endIndex < posts.length
  118. };
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment