-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
225 lines (200 loc) · 7.36 KB
/
content.js
File metadata and controls
225 lines (200 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* WebMD Content Script - HTML to Markdown Converter
*
* This content script handles the core conversion logic for transforming
* web pages into clean Markdown format. It uses Mozilla's Readability.js
* for intelligent article extraction and Turndown.js for HTML-to-Markdown
* conversion.
*/
(function() {
'use strict';
/**
* Initialize Turndown service with GitHub Flavored Markdown settings
* @type {TurndownService}
*/
const turndownService = new TurndownService({
headingStyle: 'atx',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
emDelimiter: '_',
strongDelimiter: '**',
linkStyle: 'inlined',
linkReferenceStyle: 'full',
preformattedCode: true
});
// Enable GitHub Flavored Markdown plugin for enhanced formatting support
// This adds support for tables, strikethrough text, and task lists
turndownService.use(turndownPluginGfm.gfm);
/**
* Custom Turndown rule for handling code blocks with language hints
* Preserves syntax highlighting information from class attributes
*/
turndownService.addRule('fencedCodeBlock', {
filter: function (node, options) {
return (
options.codeBlockStyle === 'fenced' &&
node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE'
);
},
replacement: function (content, node, options) {
const className = node.firstChild.getAttribute('class') || '';
const language = className.match(/language-(\S+)/);
const fence = '```';
const fenceLanguage = language ? language[1] : '';
return (
'\n\n' + fence + fenceLanguage + '\n' +
node.firstChild.textContent +
'\n' + fence + '\n\n'
);
}
});
/**
* Attempts to extract main article content using Mozilla's Readability
* @returns {Object|null} Article object with content and metadata, or null if extraction fails
*/
function extractArticleContent() {
try {
// Clone the document to avoid modifying the original
const documentClone = document.cloneNode(true);
const reader = new Readability(documentClone);
const article = reader.parse();
if (article && article.content) {
return {
title: article.title,
author: article.byline,
content: article.content,
excerpt: article.excerpt,
siteName: article.siteName,
isArticle: true
};
}
} catch (error) {
console.log('Readability extraction failed:', error);
}
return null;
}
/**
* Main conversion function that transforms the current page to Markdown
* Uses Readability for article extraction when possible, falls back to
* full page conversion for non-article content
* @returns {Object} Object containing markdown string and metadata
*/
function convertToMarkdown() {
let markdown = '';
let metadata = {};
// First, try to extract article content with Readability
const article = extractArticleContent();
if (article && article.isArticle) {
// Article successfully extracted - use Readability's clean content
// Collect all available metadata for frontmatter
metadata = {
title: article.title,
author: article.author,
siteName: article.siteName,
excerpt: article.excerpt
};
// Add metadata as YAML frontmatter
markdown = '---\n';
if (metadata.title) markdown += `title: "${metadata.title}"\n`;
if (metadata.author) markdown += `author: "${metadata.author}"\n`;
if (metadata.siteName) markdown += `site: "${metadata.siteName}"\n`;
markdown += `url: "${window.location.href}"\n`;
markdown += `date: "${new Date().toISOString()}"\n`;
markdown += '---\n\n';
// Add title as H1 if it exists
if (metadata.title) {
markdown += `# ${metadata.title}\n\n`;
}
// Convert the clean article content
const tempDiv = document.createElement('div');
tempDiv.innerHTML = article.content;
markdown += turndownService.turndown(tempDiv);
} else {
// No article detected - fall back to full page conversion
// Try to find main content area using common semantic selectors
// Listed in order of specificity and likelihood of containing main content
const contentSelectors = [
'main',
'article',
'[role="main"]',
'#content',
'.content',
'#main',
'.main'
];
let contentElement = null;
for (const selector of contentSelectors) {
contentElement = document.querySelector(selector);
if (contentElement) break;
}
// If no main content found, use body
if (!contentElement) {
contentElement = document.body;
}
// Clone the content element to avoid modifying the actual page DOM
const contentClone = contentElement.cloneNode(true);
// Remove non-content elements that would create noise in Markdown
// This includes scripts, styles, and noscript tags
const scripts = contentClone.querySelectorAll('script, style, noscript');
scripts.forEach(el => el.remove());
// Remove visually hidden elements that shouldn't appear in text output
// Targets inline styles and hidden attribute
const hidden = contentClone.querySelectorAll('[style*="display:none"], [style*="display: none"], [hidden]');
hidden.forEach(el => el.remove());
// Add basic metadata
markdown = '---\n';
markdown += `title: "${document.title}"\n`;
markdown += `url: "${window.location.href}"\n`;
markdown += `date: "${new Date().toISOString()}"\n`;
markdown += '---\n\n';
// Add page title
markdown += `# ${document.title}\n\n`;
// Convert content
markdown += turndownService.turndown(contentClone);
}
return {
markdown: markdown,
metadata: metadata
};
}
/**
* Message listener for conversion requests from popup or background script
* Handles the 'convertToMarkdown' action and sends back the result
*/
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'convertToMarkdown') {
try {
const result = convertToMarkdown();
sendResponse({ success: true, data: result });
} catch (error) {
console.error('Conversion error:', error);
sendResponse({ success: false, error: error.message });
}
}
// Return true to indicate we'll send response asynchronously
// This keeps the message channel open for sendResponse
return true;
});
/**
* Keyboard event listener for direct shortcut handling (legacy fallback)
* Note: Primary keyboard handling is done via Chrome Commands API in background.js
* This remains for backward compatibility
*/
document.addEventListener('keydown', (e) => {
// Cmd+M+D on Mac, Ctrl+Shift+M on others
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
const correctModifiers = isMac
? (e.metaKey && e.key === 'd' && e.code === 'KeyD')
: (e.ctrlKey && e.shiftKey && e.key === 'M');
if (correctModifiers) {
e.preventDefault();
const result = convertToMarkdown();
chrome.runtime.sendMessage({
action: 'openMarkdownTab',
data: result
});
}
});
})();