Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export comments #184

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions addon/appsscript.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"timeZone": "America/Los_Angeles",
"dependencies": {},
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Drive",
"serviceId": "drive",
"version": "v2"
}]
},
"oauthScopes": [
"https://www.googleapis.com/auth/documents.currentonly",
"https://www.googleapis.com/auth/script.container.ui"
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/drive.readonly"
],
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "DEPRECATED_ES5"
}
}
86 changes: 86 additions & 0 deletions addon/comments.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Functions for handling comments from Drive API

function processCommentReplies(reply) {
return {
content: reply.content,
author: reply.author.displayName,
created: reply.createdTime,
};
}

function getDocumentComments() {
const doc = DocumentApp.getActiveDocument();
const docId = doc.getId();

try {
const commentsResponse = Drive.Comments.list(docId, {
maxResults: 100,
fields: "*",
});

if (!commentsResponse.comments) return [];

return commentsResponse.comments.map((comment) => ({
id: comment.id,
content: cleanHtmlContent(comment.content),
author: comment.author.displayName,
created: comment.createdTime,
quotedText: comment.quotedFileContent
? cleanHtmlContent(comment.quotedFileContent.value)
: null,
anchor: comment.anchor,
replies: (comment.replies || []).map(processCommentReplies),
}));
} catch (e) {
Logger.log("Error getting comments:", e.toString());
return [];
}
}

// This helper formats comments for Markdown output
function formatMarkdownComment(comment, referenceId) {
let output = `[${referenceId}] **${comment.author}** (${new Date(
comment.created
).toLocaleString()}): ${comment.content}\n`;

if (comment.replies && comment.replies.length > 0) {
comment.replies.forEach((reply) => {
output += ` > **${reply.author}** (${new Date(
reply.created
).toLocaleString()}): ${reply.content}\n`;
});
}

return output;
}

// This helper formats comments for HTML output
function formatHtmlComment(comment, referenceId) {
let output = `<div class="comment" id="comment${referenceId}">\n`;
output += ` <p><strong>${comment.author}</strong> (${new Date(
comment.created
).toLocaleString()}): ${comment.content}</p>\n`;

if (comment.replies && comment.replies.length > 0) {
comment.replies.forEach((reply) => {
output += ` <blockquote><p><strong>${reply.author}</strong> (${new Date(
reply.created
).toLocaleString()}): ${reply.content}</p></blockquote>\n`;
});
}

output += `</div>\n`;
return output;
}

// Clean HTML content in comment text
function cleanHtmlContent(content) {
if (!content) return "";
return content
.replace(/<[^>]*>/g, "") // Remove HTML tags
.replace(/&quot;/g, '"') // Fix quotes
.replace(/&#39;/g, "'") // Fix apostrophes
.replace(/&amp;/g, "&") // Fix ampersands
.replace(/\s+/g, " ") // Normalize whitespace
.trim();
}
Loading