修复快照完整性:raw/llm_wiki 由 gitlink 转为普通目录(.git 备份为 .git.bak),新增 .gitignore 排除 __pycache__/pyc 与子仓库元数据
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,803 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||||
typeof define === 'function' && define.amd ? define(factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.TurndownService = factory());
|
||||
})(this, (function () { 'use strict';
|
||||
|
||||
function extend(destination) {
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
var source = arguments[i];
|
||||
for (var key in source) {
|
||||
if (Object.prototype.hasOwnProperty.call(source, key)) destination[key] = source[key];
|
||||
}
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
function repeat(character, count) {
|
||||
return Array(count + 1).join(character);
|
||||
}
|
||||
function trimLeadingNewlines(string) {
|
||||
return string.replace(/^\n*/, '');
|
||||
}
|
||||
function trimTrailingNewlines(string) {
|
||||
// avoid match-at-end regexp bottleneck, see #370
|
||||
var indexEnd = string.length;
|
||||
while (indexEnd > 0 && string[indexEnd - 1] === '\n') indexEnd--;
|
||||
return string.substring(0, indexEnd);
|
||||
}
|
||||
function trimNewlines(string) {
|
||||
return trimTrailingNewlines(trimLeadingNewlines(string));
|
||||
}
|
||||
var blockElements = ['ADDRESS', 'ARTICLE', 'ASIDE', 'AUDIO', 'BLOCKQUOTE', 'BODY', 'CANVAS', 'CENTER', 'DD', 'DIR', 'DIV', 'DL', 'DT', 'FIELDSET', 'FIGCAPTION', 'FIGURE', 'FOOTER', 'FORM', 'FRAMESET', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEADER', 'HGROUP', 'HR', 'HTML', 'ISINDEX', 'LI', 'MAIN', 'MENU', 'NAV', 'NOFRAMES', 'NOSCRIPT', 'OL', 'OUTPUT', 'P', 'PRE', 'SECTION', 'TABLE', 'TBODY', 'TD', 'TFOOT', 'TH', 'THEAD', 'TR', 'UL'];
|
||||
function isBlock(node) {
|
||||
return is(node, blockElements);
|
||||
}
|
||||
var voidElements = ['AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT', 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR'];
|
||||
function isVoid(node) {
|
||||
return is(node, voidElements);
|
||||
}
|
||||
function hasVoid(node) {
|
||||
return has(node, voidElements);
|
||||
}
|
||||
var meaningfulWhenBlankElements = ['A', 'TABLE', 'THEAD', 'TBODY', 'TFOOT', 'TH', 'TD', 'IFRAME', 'SCRIPT', 'AUDIO', 'VIDEO'];
|
||||
function isMeaningfulWhenBlank(node) {
|
||||
return is(node, meaningfulWhenBlankElements);
|
||||
}
|
||||
function hasMeaningfulWhenBlank(node) {
|
||||
return has(node, meaningfulWhenBlankElements);
|
||||
}
|
||||
function is(node, tagNames) {
|
||||
return tagNames.indexOf(node.nodeName) >= 0;
|
||||
}
|
||||
function has(node, tagNames) {
|
||||
return node.getElementsByTagName && tagNames.some(function (tagName) {
|
||||
return node.getElementsByTagName(tagName).length;
|
||||
});
|
||||
}
|
||||
var markdownEscapes = [[/\\/g, '\\\\'], [/\*/g, '\\*'], [/^-/g, '\\-'], [/^\+ /g, '\\+ '], [/^(=+)/g, '\\$1'], [/^(#{1,6}) /g, '\\$1 '], [/`/g, '\\`'], [/^~~~/g, '\\~~~'], [/\[/g, '\\['], [/\]/g, '\\]'], [/^>/g, '\\>'], [/_/g, '\\_'], [/^(\d+)\. /g, '$1\\. ']];
|
||||
function escapeMarkdown(string) {
|
||||
return markdownEscapes.reduce(function (accumulator, escape) {
|
||||
return accumulator.replace(escape[0], escape[1]);
|
||||
}, string);
|
||||
}
|
||||
|
||||
var rules = {};
|
||||
rules.paragraph = {
|
||||
filter: 'p',
|
||||
replacement: function (content) {
|
||||
return '\n\n' + content + '\n\n';
|
||||
}
|
||||
};
|
||||
rules.lineBreak = {
|
||||
filter: 'br',
|
||||
replacement: function (content, node, options) {
|
||||
return options.br + '\n';
|
||||
}
|
||||
};
|
||||
rules.heading = {
|
||||
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
||||
replacement: function (content, node, options) {
|
||||
var hLevel = Number(node.nodeName.charAt(1));
|
||||
if (options.headingStyle === 'setext' && hLevel < 3) {
|
||||
var underline = repeat(hLevel === 1 ? '=' : '-', content.length);
|
||||
return '\n\n' + content + '\n' + underline + '\n\n';
|
||||
} else {
|
||||
return '\n\n' + repeat('#', hLevel) + ' ' + content + '\n\n';
|
||||
}
|
||||
}
|
||||
};
|
||||
rules.blockquote = {
|
||||
filter: 'blockquote',
|
||||
replacement: function (content) {
|
||||
content = trimNewlines(content).replace(/^/gm, '> ');
|
||||
return '\n\n' + content + '\n\n';
|
||||
}
|
||||
};
|
||||
rules.list = {
|
||||
filter: ['ul', 'ol'],
|
||||
replacement: function (content, node) {
|
||||
var parent = node.parentNode;
|
||||
if (parent.nodeName === 'LI' && parent.lastElementChild === node) {
|
||||
return '\n' + content;
|
||||
} else {
|
||||
return '\n\n' + content + '\n\n';
|
||||
}
|
||||
}
|
||||
};
|
||||
rules.listItem = {
|
||||
filter: 'li',
|
||||
replacement: function (content, node, options) {
|
||||
var prefix = options.bulletListMarker + ' ';
|
||||
var parent = node.parentNode;
|
||||
if (parent.nodeName === 'OL') {
|
||||
var start = parent.getAttribute('start');
|
||||
var index = Array.prototype.indexOf.call(parent.children, node);
|
||||
prefix = (start ? Number(start) + index : index + 1) + '. ';
|
||||
}
|
||||
var isParagraph = /\n$/.test(content);
|
||||
content = trimNewlines(content) + (isParagraph ? '\n' : '');
|
||||
content = content.replace(/\n/gm, '\n' + ' '.repeat(prefix.length)); // indent
|
||||
return prefix + content + (node.nextSibling ? '\n' : '');
|
||||
}
|
||||
};
|
||||
rules.indentedCodeBlock = {
|
||||
filter: function (node, options) {
|
||||
return options.codeBlockStyle === 'indented' && node.nodeName === 'PRE' && node.firstChild && node.firstChild.nodeName === 'CODE';
|
||||
},
|
||||
replacement: function (content, node, options) {
|
||||
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n';
|
||||
}
|
||||
};
|
||||
rules.fencedCodeBlock = {
|
||||
filter: function (node, options) {
|
||||
return options.codeBlockStyle === 'fenced' && node.nodeName === 'PRE' && node.firstChild && node.firstChild.nodeName === 'CODE';
|
||||
},
|
||||
replacement: function (content, node, options) {
|
||||
var className = node.firstChild.getAttribute('class') || '';
|
||||
var language = (className.match(/language-(\S+)/) || [null, ''])[1];
|
||||
var code = node.firstChild.textContent;
|
||||
var fenceChar = options.fence.charAt(0);
|
||||
var fenceSize = 3;
|
||||
var fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm');
|
||||
var match;
|
||||
while (match = fenceInCodeRegex.exec(code)) {
|
||||
if (match[0].length >= fenceSize) {
|
||||
fenceSize = match[0].length + 1;
|
||||
}
|
||||
}
|
||||
var fence = repeat(fenceChar, fenceSize);
|
||||
return '\n\n' + fence + language + '\n' + code.replace(/\n$/, '') + '\n' + fence + '\n\n';
|
||||
}
|
||||
};
|
||||
rules.horizontalRule = {
|
||||
filter: 'hr',
|
||||
replacement: function (content, node, options) {
|
||||
return '\n\n' + options.hr + '\n\n';
|
||||
}
|
||||
};
|
||||
rules.inlineLink = {
|
||||
filter: function (node, options) {
|
||||
return options.linkStyle === 'inlined' && node.nodeName === 'A' && node.getAttribute('href');
|
||||
},
|
||||
replacement: function (content, node) {
|
||||
var href = escapeLinkDestination(node.getAttribute('href'));
|
||||
var title = escapeLinkTitle(cleanAttribute(node.getAttribute('title')));
|
||||
var titlePart = title ? ' "' + title + '"' : '';
|
||||
return '[' + content + '](' + href + titlePart + ')';
|
||||
}
|
||||
};
|
||||
rules.referenceLink = {
|
||||
filter: function (node, options) {
|
||||
return options.linkStyle === 'referenced' && node.nodeName === 'A' && node.getAttribute('href');
|
||||
},
|
||||
replacement: function (content, node, options) {
|
||||
var href = escapeLinkDestination(node.getAttribute('href'));
|
||||
var title = cleanAttribute(node.getAttribute('title'));
|
||||
if (title) title = ' "' + escapeLinkTitle(title) + '"';
|
||||
var replacement;
|
||||
var reference;
|
||||
switch (options.linkReferenceStyle) {
|
||||
case 'collapsed':
|
||||
replacement = '[' + content + '][]';
|
||||
reference = '[' + content + ']: ' + href + title;
|
||||
break;
|
||||
case 'shortcut':
|
||||
replacement = '[' + content + ']';
|
||||
reference = '[' + content + ']: ' + href + title;
|
||||
break;
|
||||
default:
|
||||
var id = this.references.length + 1;
|
||||
replacement = '[' + content + '][' + id + ']';
|
||||
reference = '[' + id + ']: ' + href + title;
|
||||
}
|
||||
this.references.push(reference);
|
||||
return replacement;
|
||||
},
|
||||
references: [],
|
||||
append: function (options) {
|
||||
var references = '';
|
||||
if (this.references.length) {
|
||||
references = '\n\n' + this.references.join('\n') + '\n\n';
|
||||
this.references = []; // Reset references
|
||||
}
|
||||
return references;
|
||||
}
|
||||
};
|
||||
rules.emphasis = {
|
||||
filter: ['em', 'i'],
|
||||
replacement: function (content, node, options) {
|
||||
if (!content.trim()) return '';
|
||||
return options.emDelimiter + content + options.emDelimiter;
|
||||
}
|
||||
};
|
||||
rules.strong = {
|
||||
filter: ['strong', 'b'],
|
||||
replacement: function (content, node, options) {
|
||||
if (!content.trim()) return '';
|
||||
return options.strongDelimiter + content + options.strongDelimiter;
|
||||
}
|
||||
};
|
||||
rules.code = {
|
||||
filter: function (node) {
|
||||
var hasSiblings = node.previousSibling || node.nextSibling;
|
||||
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
|
||||
return node.nodeName === 'CODE' && !isCodeBlock;
|
||||
},
|
||||
replacement: function (content) {
|
||||
if (!content) return '';
|
||||
content = content.replace(/\r?\n|\r/g, ' ');
|
||||
var extraSpace = /^`|^ .*?[^ ].* $|`$/.test(content) ? ' ' : '';
|
||||
var delimiter = '`';
|
||||
var matches = content.match(/`+/gm) || [];
|
||||
while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`';
|
||||
return delimiter + extraSpace + content + extraSpace + delimiter;
|
||||
}
|
||||
};
|
||||
rules.image = {
|
||||
filter: 'img',
|
||||
replacement: function (content, node) {
|
||||
var alt = escapeMarkdown(cleanAttribute(node.getAttribute('alt')));
|
||||
var src = escapeLinkDestination(node.getAttribute('src') || '');
|
||||
var title = cleanAttribute(node.getAttribute('title'));
|
||||
var titlePart = title ? ' "' + escapeLinkTitle(title) + '"' : '';
|
||||
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : '';
|
||||
}
|
||||
};
|
||||
function cleanAttribute(attribute) {
|
||||
return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : '';
|
||||
}
|
||||
function escapeLinkDestination(destination) {
|
||||
var escaped = destination.replace(/([<>()])/g, '\\$1');
|
||||
return escaped.indexOf(' ') >= 0 ? '<' + escaped + '>' : escaped;
|
||||
}
|
||||
function escapeLinkTitle(title) {
|
||||
return title.replace(/"/g, '\\"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages a collection of rules used to convert HTML to Markdown
|
||||
*/
|
||||
|
||||
function Rules(options) {
|
||||
this.options = options;
|
||||
this._keep = [];
|
||||
this._remove = [];
|
||||
this.blankRule = {
|
||||
replacement: options.blankReplacement
|
||||
};
|
||||
this.keepReplacement = options.keepReplacement;
|
||||
this.defaultRule = {
|
||||
replacement: options.defaultReplacement
|
||||
};
|
||||
this.array = [];
|
||||
for (var key in options.rules) this.array.push(options.rules[key]);
|
||||
}
|
||||
Rules.prototype = {
|
||||
add: function (key, rule) {
|
||||
this.array.unshift(rule);
|
||||
},
|
||||
keep: function (filter) {
|
||||
this._keep.unshift({
|
||||
filter: filter,
|
||||
replacement: this.keepReplacement
|
||||
});
|
||||
},
|
||||
remove: function (filter) {
|
||||
this._remove.unshift({
|
||||
filter: filter,
|
||||
replacement: function () {
|
||||
return '';
|
||||
}
|
||||
});
|
||||
},
|
||||
forNode: function (node) {
|
||||
if (node.isBlank) return this.blankRule;
|
||||
var rule;
|
||||
if (rule = findRule(this.array, node, this.options)) return rule;
|
||||
if (rule = findRule(this._keep, node, this.options)) return rule;
|
||||
if (rule = findRule(this._remove, node, this.options)) return rule;
|
||||
return this.defaultRule;
|
||||
},
|
||||
forEach: function (fn) {
|
||||
for (var i = 0; i < this.array.length; i++) fn(this.array[i], i);
|
||||
}
|
||||
};
|
||||
function findRule(rules, node, options) {
|
||||
for (var i = 0; i < rules.length; i++) {
|
||||
var rule = rules[i];
|
||||
if (filterValue(rule, node, options)) return rule;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
function filterValue(rule, node, options) {
|
||||
var filter = rule.filter;
|
||||
if (typeof filter === 'string') {
|
||||
if (filter === node.nodeName.toLowerCase()) return true;
|
||||
} else if (Array.isArray(filter)) {
|
||||
if (filter.indexOf(node.nodeName.toLowerCase()) > -1) return true;
|
||||
} else if (typeof filter === 'function') {
|
||||
if (filter.call(rule, node, options)) return true;
|
||||
} else {
|
||||
throw new TypeError('`filter` needs to be a string, array, or function');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The collapseWhitespace function is adapted from collapse-whitespace
|
||||
* by Luc Thevenard.
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* collapseWhitespace(options) removes extraneous whitespace from an the given element.
|
||||
*
|
||||
* @param {Object} options
|
||||
*/
|
||||
function collapseWhitespace(options) {
|
||||
var element = options.element;
|
||||
var isBlock = options.isBlock;
|
||||
var isVoid = options.isVoid;
|
||||
var isPre = options.isPre || function (node) {
|
||||
return node.nodeName === 'PRE';
|
||||
};
|
||||
if (!element.firstChild || isPre(element)) return;
|
||||
var prevText = null;
|
||||
var keepLeadingWs = false;
|
||||
var prev = null;
|
||||
var node = next(prev, element, isPre);
|
||||
while (node !== element) {
|
||||
if (node.nodeType === 3 || node.nodeType === 4) {
|
||||
// Node.TEXT_NODE or Node.CDATA_SECTION_NODE
|
||||
var text = node.data.replace(/[ \r\n\t]+/g, ' ');
|
||||
if ((!prevText || / $/.test(prevText.data)) && !keepLeadingWs && text[0] === ' ') {
|
||||
text = text.substr(1);
|
||||
}
|
||||
|
||||
// `text` might be empty at this point.
|
||||
if (!text) {
|
||||
node = remove(node);
|
||||
continue;
|
||||
}
|
||||
node.data = text;
|
||||
prevText = node;
|
||||
} else if (node.nodeType === 1) {
|
||||
// Node.ELEMENT_NODE
|
||||
if (isBlock(node) || node.nodeName === 'BR') {
|
||||
if (prevText) {
|
||||
prevText.data = prevText.data.replace(/ $/, '');
|
||||
}
|
||||
prevText = null;
|
||||
keepLeadingWs = false;
|
||||
} else if (isVoid(node) || isPre(node)) {
|
||||
// Avoid trimming space around non-block, non-BR void elements and inline PRE.
|
||||
prevText = null;
|
||||
keepLeadingWs = true;
|
||||
} else if (prevText) {
|
||||
// Drop protection if set previously.
|
||||
keepLeadingWs = false;
|
||||
}
|
||||
} else {
|
||||
node = remove(node);
|
||||
continue;
|
||||
}
|
||||
var nextNode = next(prev, node, isPre);
|
||||
prev = node;
|
||||
node = nextNode;
|
||||
}
|
||||
if (prevText) {
|
||||
prevText.data = prevText.data.replace(/ $/, '');
|
||||
if (!prevText.data) {
|
||||
remove(prevText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* remove(node) removes the given node from the DOM and returns the
|
||||
* next node in the sequence.
|
||||
*
|
||||
* @param {Node} node
|
||||
* @return {Node} node
|
||||
*/
|
||||
function remove(node) {
|
||||
var next = node.nextSibling || node.parentNode;
|
||||
node.parentNode.removeChild(node);
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* next(prev, current, isPre) returns the next node in the sequence, given the
|
||||
* current and previous nodes.
|
||||
*
|
||||
* @param {Node} prev
|
||||
* @param {Node} current
|
||||
* @param {Function} isPre
|
||||
* @return {Node}
|
||||
*/
|
||||
function next(prev, current, isPre) {
|
||||
if (prev && prev.parentNode === current || isPre(current)) {
|
||||
return current.nextSibling || current.parentNode;
|
||||
}
|
||||
return current.firstChild || current.nextSibling || current.parentNode;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set up window for Node.js
|
||||
*/
|
||||
|
||||
var root = typeof window !== 'undefined' ? window : {};
|
||||
|
||||
/*
|
||||
* Parsing HTML strings
|
||||
*/
|
||||
|
||||
function canParseHTMLNatively() {
|
||||
var Parser = root.DOMParser;
|
||||
var canParse = false;
|
||||
|
||||
// Adapted from https://gist.github.com/1129031
|
||||
// Firefox/Opera/IE throw errors on unsupported types
|
||||
try {
|
||||
// WebKit returns null on unsupported types
|
||||
if (new Parser().parseFromString('', 'text/html')) {
|
||||
canParse = true;
|
||||
}
|
||||
} catch (e) {}
|
||||
return canParse;
|
||||
}
|
||||
function createHTMLParser() {
|
||||
var Parser = function () {};
|
||||
{
|
||||
if (shouldUseActiveX()) {
|
||||
Parser.prototype.parseFromString = function (string) {
|
||||
var doc = new window.ActiveXObject('htmlfile');
|
||||
doc.designMode = 'on'; // disable on-page scripts
|
||||
doc.open();
|
||||
doc.write(string);
|
||||
doc.close();
|
||||
return doc;
|
||||
};
|
||||
} else {
|
||||
Parser.prototype.parseFromString = function (string) {
|
||||
var doc = document.implementation.createHTMLDocument('');
|
||||
doc.open();
|
||||
doc.write(string);
|
||||
doc.close();
|
||||
return doc;
|
||||
};
|
||||
}
|
||||
}
|
||||
return Parser;
|
||||
}
|
||||
function shouldUseActiveX() {
|
||||
var useActiveX = false;
|
||||
try {
|
||||
document.implementation.createHTMLDocument('').open();
|
||||
} catch (e) {
|
||||
if (root.ActiveXObject) useActiveX = true;
|
||||
}
|
||||
return useActiveX;
|
||||
}
|
||||
var HTMLParser = canParseHTMLNatively() ? root.DOMParser : createHTMLParser();
|
||||
|
||||
function RootNode(input, options) {
|
||||
var root;
|
||||
if (typeof input === 'string') {
|
||||
var doc = htmlParser().parseFromString(
|
||||
// DOM parsers arrange elements in the <head> and <body>.
|
||||
// Wrapping in a custom element ensures elements are reliably arranged in
|
||||
// a single element.
|
||||
'<x-turndown id="turndown-root">' + input + '</x-turndown>', 'text/html');
|
||||
root = doc.getElementById('turndown-root');
|
||||
} else {
|
||||
root = input.cloneNode(true);
|
||||
}
|
||||
collapseWhitespace({
|
||||
element: root,
|
||||
isBlock: isBlock,
|
||||
isVoid: isVoid,
|
||||
isPre: options.preformattedCode ? isPreOrCode : null
|
||||
});
|
||||
return root;
|
||||
}
|
||||
var _htmlParser;
|
||||
function htmlParser() {
|
||||
_htmlParser = _htmlParser || new HTMLParser();
|
||||
return _htmlParser;
|
||||
}
|
||||
function isPreOrCode(node) {
|
||||
return node.nodeName === 'PRE' || node.nodeName === 'CODE';
|
||||
}
|
||||
|
||||
function Node(node, options) {
|
||||
node.isBlock = isBlock(node);
|
||||
node.isCode = node.nodeName === 'CODE' || node.parentNode.isCode;
|
||||
node.isBlank = isBlank(node);
|
||||
node.flankingWhitespace = flankingWhitespace(node, options);
|
||||
return node;
|
||||
}
|
||||
function isBlank(node) {
|
||||
return !isVoid(node) && !isMeaningfulWhenBlank(node) && /^\s*$/i.test(node.textContent) && !hasVoid(node) && !hasMeaningfulWhenBlank(node);
|
||||
}
|
||||
function flankingWhitespace(node, options) {
|
||||
if (node.isBlock || options.preformattedCode && node.isCode) {
|
||||
return {
|
||||
leading: '',
|
||||
trailing: ''
|
||||
};
|
||||
}
|
||||
var edges = edgeWhitespace(node.textContent);
|
||||
|
||||
// abandon leading ASCII WS if left-flanked by ASCII WS
|
||||
if (edges.leadingAscii && isFlankedByWhitespace('left', node, options)) {
|
||||
edges.leading = edges.leadingNonAscii;
|
||||
}
|
||||
|
||||
// abandon trailing ASCII WS if right-flanked by ASCII WS
|
||||
if (edges.trailingAscii && isFlankedByWhitespace('right', node, options)) {
|
||||
edges.trailing = edges.trailingNonAscii;
|
||||
}
|
||||
return {
|
||||
leading: edges.leading,
|
||||
trailing: edges.trailing
|
||||
};
|
||||
}
|
||||
function edgeWhitespace(string) {
|
||||
var m = string.match(/^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/);
|
||||
return {
|
||||
leading: m[1],
|
||||
// whole string for whitespace-only strings
|
||||
leadingAscii: m[2],
|
||||
leadingNonAscii: m[3],
|
||||
trailing: m[4],
|
||||
// empty for whitespace-only strings
|
||||
trailingNonAscii: m[5],
|
||||
trailingAscii: m[6]
|
||||
};
|
||||
}
|
||||
function isFlankedByWhitespace(side, node, options) {
|
||||
var sibling;
|
||||
var regExp;
|
||||
var isFlanked;
|
||||
if (side === 'left') {
|
||||
sibling = node.previousSibling;
|
||||
regExp = / $/;
|
||||
} else {
|
||||
sibling = node.nextSibling;
|
||||
regExp = /^ /;
|
||||
}
|
||||
if (sibling) {
|
||||
if (sibling.nodeType === 3) {
|
||||
isFlanked = regExp.test(sibling.nodeValue);
|
||||
} else if (options.preformattedCode && sibling.nodeName === 'CODE') {
|
||||
isFlanked = false;
|
||||
} else if (sibling.nodeType === 1 && !isBlock(sibling)) {
|
||||
isFlanked = regExp.test(sibling.textContent);
|
||||
}
|
||||
}
|
||||
return isFlanked;
|
||||
}
|
||||
|
||||
var reduce = Array.prototype.reduce;
|
||||
function TurndownService(options) {
|
||||
if (!(this instanceof TurndownService)) return new TurndownService(options);
|
||||
var defaults = {
|
||||
rules: rules,
|
||||
headingStyle: 'setext',
|
||||
hr: '* * *',
|
||||
bulletListMarker: '*',
|
||||
codeBlockStyle: 'indented',
|
||||
fence: '```',
|
||||
emDelimiter: '_',
|
||||
strongDelimiter: '**',
|
||||
linkStyle: 'inlined',
|
||||
linkReferenceStyle: 'full',
|
||||
br: ' ',
|
||||
preformattedCode: false,
|
||||
blankReplacement: function (content, node) {
|
||||
return node.isBlock ? '\n\n' : '';
|
||||
},
|
||||
keepReplacement: function (content, node) {
|
||||
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML;
|
||||
},
|
||||
defaultReplacement: function (content, node) {
|
||||
return node.isBlock ? '\n\n' + content + '\n\n' : content;
|
||||
}
|
||||
};
|
||||
this.options = extend({}, defaults, options);
|
||||
this.rules = new Rules(this.options);
|
||||
}
|
||||
TurndownService.prototype = {
|
||||
/**
|
||||
* The entry point for converting a string or DOM node to Markdown
|
||||
* @public
|
||||
* @param {String|HTMLElement} input The string or DOM node to convert
|
||||
* @returns A Markdown representation of the input
|
||||
* @type String
|
||||
*/
|
||||
|
||||
turndown: function (input) {
|
||||
if (!canConvert(input)) {
|
||||
throw new TypeError(input + ' is not a string, or an element/document/fragment node.');
|
||||
}
|
||||
if (input === '') return '';
|
||||
var output = process.call(this, new RootNode(input, this.options));
|
||||
return postProcess.call(this, output);
|
||||
},
|
||||
/**
|
||||
* Add one or more plugins
|
||||
* @public
|
||||
* @param {Function|Array} plugin The plugin or array of plugins to add
|
||||
* @returns The Turndown instance for chaining
|
||||
* @type Object
|
||||
*/
|
||||
|
||||
use: function (plugin) {
|
||||
if (Array.isArray(plugin)) {
|
||||
for (var i = 0; i < plugin.length; i++) this.use(plugin[i]);
|
||||
} else if (typeof plugin === 'function') {
|
||||
plugin(this);
|
||||
} else {
|
||||
throw new TypeError('plugin must be a Function or an Array of Functions');
|
||||
}
|
||||
return this;
|
||||
},
|
||||
/**
|
||||
* Adds a rule
|
||||
* @public
|
||||
* @param {String} key The unique key of the rule
|
||||
* @param {Object} rule The rule
|
||||
* @returns The Turndown instance for chaining
|
||||
* @type Object
|
||||
*/
|
||||
|
||||
addRule: function (key, rule) {
|
||||
this.rules.add(key, rule);
|
||||
return this;
|
||||
},
|
||||
/**
|
||||
* Keep a node (as HTML) that matches the filter
|
||||
* @public
|
||||
* @param {String|Array|Function} filter The unique key of the rule
|
||||
* @returns The Turndown instance for chaining
|
||||
* @type Object
|
||||
*/
|
||||
|
||||
keep: function (filter) {
|
||||
this.rules.keep(filter);
|
||||
return this;
|
||||
},
|
||||
/**
|
||||
* Remove a node that matches the filter
|
||||
* @public
|
||||
* @param {String|Array|Function} filter The unique key of the rule
|
||||
* @returns The Turndown instance for chaining
|
||||
* @type Object
|
||||
*/
|
||||
|
||||
remove: function (filter) {
|
||||
this.rules.remove(filter);
|
||||
return this;
|
||||
},
|
||||
/**
|
||||
* Escapes Markdown syntax
|
||||
* @public
|
||||
* @param {String} string The string to escape
|
||||
* @returns A string with Markdown syntax escaped
|
||||
* @type String
|
||||
*/
|
||||
|
||||
escape: function (string) {
|
||||
return escapeMarkdown(string);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Reduces a DOM node down to its Markdown string equivalent
|
||||
* @private
|
||||
* @param {HTMLElement} parentNode The node to convert
|
||||
* @returns A Markdown representation of the node
|
||||
* @type String
|
||||
*/
|
||||
|
||||
function process(parentNode) {
|
||||
var self = this;
|
||||
return reduce.call(parentNode.childNodes, function (output, node) {
|
||||
node = new Node(node, self.options);
|
||||
var replacement = '';
|
||||
if (node.nodeType === 3) {
|
||||
replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue);
|
||||
} else if (node.nodeType === 1) {
|
||||
replacement = replacementForNode.call(self, node);
|
||||
}
|
||||
return join(output, replacement);
|
||||
}, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends strings as each rule requires and trims the output
|
||||
* @private
|
||||
* @param {String} output The conversion output
|
||||
* @returns A trimmed version of the ouput
|
||||
* @type String
|
||||
*/
|
||||
|
||||
function postProcess(output) {
|
||||
var self = this;
|
||||
this.rules.forEach(function (rule) {
|
||||
if (typeof rule.append === 'function') {
|
||||
output = join(output, rule.append(self.options));
|
||||
}
|
||||
});
|
||||
return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an element node to its Markdown equivalent
|
||||
* @private
|
||||
* @param {HTMLElement} node The node to convert
|
||||
* @returns A Markdown representation of the node
|
||||
* @type String
|
||||
*/
|
||||
|
||||
function replacementForNode(node) {
|
||||
var rule = this.rules.forNode(node);
|
||||
var content = process.call(this, node);
|
||||
var whitespace = node.flankingWhitespace;
|
||||
if (whitespace.leading || whitespace.trailing) content = content.trim();
|
||||
return whitespace.leading + rule.replacement(content, node, this.options) + whitespace.trailing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins replacement to the current output with appropriate number of new lines
|
||||
* @private
|
||||
* @param {String} output The current conversion output
|
||||
* @param {String} replacement The string to append to the output
|
||||
* @returns Joined output
|
||||
* @type String
|
||||
*/
|
||||
|
||||
function join(output, replacement) {
|
||||
var s1 = trimTrailingNewlines(output);
|
||||
var s2 = trimLeadingNewlines(replacement);
|
||||
var nls = Math.max(output.length - s1.length, replacement.length - s2.length);
|
||||
var separator = '\n\n'.substring(0, nls);
|
||||
return s1 + separator + s2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether an input can be converted
|
||||
* @private
|
||||
* @param {String|HTMLElement} input Describe this parameter
|
||||
* @returns Describe what it returns
|
||||
* @type String|Object|Array|Boolean|Number
|
||||
*/
|
||||
|
||||
function canConvert(input) {
|
||||
return input != null && (typeof input === 'string' || input.nodeType && (input.nodeType === 1 || input.nodeType === 9 || input.nodeType === 11));
|
||||
}
|
||||
|
||||
return TurndownService;
|
||||
|
||||
}));
|
||||
@@ -0,0 +1,56 @@
|
||||
importScripts("clipper-core.js");
|
||||
|
||||
const COMMAND_NAME = "clip-current-page";
|
||||
let badgeTimer;
|
||||
let clipInFlight = false;
|
||||
|
||||
async function setBadge(text, color, title, clearAfterMs = 0) {
|
||||
clearTimeout(badgeTimer);
|
||||
await chrome.action.setBadgeBackgroundColor({ color });
|
||||
await chrome.action.setBadgeText({ text });
|
||||
if (title) await chrome.action.setTitle({ title });
|
||||
if (clearAfterMs > 0) {
|
||||
badgeTimer = setTimeout(() => {
|
||||
void chrome.action.setBadgeText({ text: "" });
|
||||
void chrome.action.setTitle({ title: "LLM Wiki Clipper" });
|
||||
}, clearAfterMs);
|
||||
}
|
||||
}
|
||||
|
||||
async function clipCurrentPage(commandTab) {
|
||||
if (clipInFlight) {
|
||||
await setBadge("…", "#4f46e5", "A page clip is already in progress");
|
||||
return;
|
||||
}
|
||||
clipInFlight = true;
|
||||
const core = globalThis.LLMWikiClipper;
|
||||
try {
|
||||
await setBadge("…", "#4f46e5", "Clipping current page...");
|
||||
const settings = await core.loadSettings();
|
||||
const connection = {
|
||||
serverUrl: settings.serverUrl,
|
||||
accessToken: settings.accessToken,
|
||||
};
|
||||
const { projects, baseUrl } = await core.loadProjects(connection);
|
||||
connection.serverUrl = baseUrl;
|
||||
const project = core.selectProject(projects, settings.preferredProjectPath);
|
||||
if (!project) throw new Error("No LLM Wiki project is available");
|
||||
|
||||
const page = await core.extractActiveTab(commandTab);
|
||||
const submitted = await core.submitClip(page, project.path, connection);
|
||||
await chrome.storage.local.set({
|
||||
serverUrl: submitted.baseUrl,
|
||||
});
|
||||
await setBadge("✓", "#059669", `Saved to ${project.name || "LLM Wiki"}`, 4000);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error("[LLM Wiki Clipper] shortcut failed:", error);
|
||||
await setBadge("!", "#dc2626", `Clip failed: ${message}`, 7000);
|
||||
} finally {
|
||||
clipInFlight = false;
|
||||
}
|
||||
}
|
||||
|
||||
chrome.commands.onCommand.addListener((command, tab) => {
|
||||
if (command === COMMAND_NAME) void clipCurrentPage(tab);
|
||||
});
|
||||
@@ -0,0 +1,226 @@
|
||||
(function initializeClipperCore(global) {
|
||||
const DEFAULT_API_URLS = ["http://127.0.0.1:19827", "http://localhost:19827"];
|
||||
const MAX_EXTRACTED_CONTENT_CHARS = 1_000_000;
|
||||
const TRUNCATION_NOTICE = "\n\n[LLM Wiki Clipper: page content truncated at 1,000,000 characters.]";
|
||||
|
||||
function limitExtractedContent(content) {
|
||||
const value = String(content || "");
|
||||
if (value.length <= MAX_EXTRACTED_CONTENT_CHARS) return value;
|
||||
return `${value.slice(0, MAX_EXTRACTED_CONTENT_CHARS)}${TRUNCATION_NOTICE}`;
|
||||
}
|
||||
|
||||
function normalizeServerUrl(value) {
|
||||
let candidate = String(value || "").trim();
|
||||
if (!candidate) return DEFAULT_API_URLS[0];
|
||||
if (!/^https?:\/\//i.test(candidate)) candidate = `http://${candidate}`;
|
||||
const parsed = new URL(candidate);
|
||||
if (!/^https?:$/.test(parsed.protocol) || parsed.username || parsed.password) {
|
||||
throw new Error("Use an http(s) address without embedded credentials");
|
||||
}
|
||||
if (parsed.pathname !== "/" || parsed.search || parsed.hash) {
|
||||
throw new Error("Enter only the server origin, without a path, query, or fragment");
|
||||
}
|
||||
if (!parsed.port) parsed.port = "19827";
|
||||
return parsed.origin;
|
||||
}
|
||||
|
||||
async function loadSettings() {
|
||||
const saved = await chrome.storage.local.get([
|
||||
"serverUrl",
|
||||
"accessToken",
|
||||
"preferredProjectPath",
|
||||
]);
|
||||
let serverUrl;
|
||||
try {
|
||||
serverUrl = normalizeServerUrl(saved.serverUrl || DEFAULT_API_URLS[0]);
|
||||
} catch {
|
||||
serverUrl = DEFAULT_API_URLS[0];
|
||||
}
|
||||
return {
|
||||
serverUrl,
|
||||
accessToken: String(saved.accessToken || ""),
|
||||
preferredProjectPath: String(saved.preferredProjectPath || ""),
|
||||
};
|
||||
}
|
||||
|
||||
function requestHeaders(accessToken, options) {
|
||||
const headers = new Headers(options?.headers || {});
|
||||
if (accessToken) headers.set("Authorization", `Bearer ${accessToken}`);
|
||||
return headers;
|
||||
}
|
||||
|
||||
async function clipFetch(path, options, connection) {
|
||||
const method = String(options?.method || "GET").toUpperCase();
|
||||
const serverUrl = normalizeServerUrl(connection?.serverUrl || DEFAULT_API_URLS[0]);
|
||||
// A POST is never retried because the first request may have reached the
|
||||
// Clip Server even when its response was lost.
|
||||
const isDefaultLocalAddress = DEFAULT_API_URLS.includes(serverUrl);
|
||||
const urls = method === "GET" && isDefaultLocalAddress
|
||||
? [serverUrl, ...DEFAULT_API_URLS.filter((url) => url !== serverUrl)]
|
||||
: [serverUrl];
|
||||
let lastError;
|
||||
|
||||
for (const baseUrl of urls) {
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}${path}`, {
|
||||
...options,
|
||||
headers: requestHeaders(connection?.accessToken, options),
|
||||
});
|
||||
return { response, baseUrl };
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
}
|
||||
}
|
||||
throw lastError || new Error("Unable to connect to LLM Wiki");
|
||||
}
|
||||
|
||||
// This function is serialized into the active tab by chrome.scripting, so it
|
||||
// must remain self-contained and must not capture extension-scope variables.
|
||||
function extractReadablePage() {
|
||||
try {
|
||||
const documentClone = document.cloneNode(true);
|
||||
const reader = new window.Readability(documentClone);
|
||||
const article = reader.parse();
|
||||
if (!article || !article.content) {
|
||||
return { error: "Readability could not extract content" };
|
||||
}
|
||||
|
||||
const turndown = new window.TurndownService({
|
||||
headingStyle: "atx",
|
||||
codeBlockStyle: "fenced",
|
||||
bulletListMarker: "-",
|
||||
});
|
||||
turndown.addRule("tableCell", {
|
||||
filter: ["th", "td"],
|
||||
replacement: (content) => ` ${content.trim()} |`,
|
||||
});
|
||||
turndown.addRule("tableRow", {
|
||||
filter: "tr",
|
||||
replacement: (content) => `|${content}\n`,
|
||||
});
|
||||
turndown.addRule("table", {
|
||||
filter: "table",
|
||||
replacement: (content) => {
|
||||
const lines = content.trim().split("\n");
|
||||
if (lines.length > 0) {
|
||||
const columns = (lines[0].match(/\|/g) || []).length - 1;
|
||||
lines.splice(1, 0, `|${" --- |".repeat(columns)}`);
|
||||
}
|
||||
return `\n\n${lines.join("\n")}\n\n`;
|
||||
},
|
||||
});
|
||||
turndown.addRule("removeSmallImages", {
|
||||
filter: (node) => {
|
||||
if (node.nodeName !== "IMG") return false;
|
||||
const width = parseInt(node.getAttribute("width") || "999");
|
||||
const height = parseInt(node.getAttribute("height") || "999");
|
||||
return width < 10 || height < 10;
|
||||
},
|
||||
replacement: () => "",
|
||||
});
|
||||
|
||||
return {
|
||||
title: article.title || document.title || "Untitled",
|
||||
content: turndown.turndown(article.content),
|
||||
excerpt: article.excerpt || "",
|
||||
};
|
||||
} catch (error) {
|
||||
return { error: error instanceof Error ? error.message : String(error) };
|
||||
}
|
||||
}
|
||||
|
||||
function extractFallbackPage() {
|
||||
const clone = document.body?.cloneNode(true);
|
||||
if (!clone) return "";
|
||||
["script", "style", "nav", "header", "footer", ".sidebar", ".ad", ".comments"]
|
||||
.forEach((selector) => clone.querySelectorAll(selector).forEach((element) => element.remove()));
|
||||
return clone.innerText
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.join("\n\n");
|
||||
}
|
||||
|
||||
async function extractActiveTab(commandTab) {
|
||||
// Chrome passes the exact shortcut target to commands.onCommand together
|
||||
// with the temporary activeTab grant. Popup callers do not have that value
|
||||
// and intentionally resolve their own currently active tab instead.
|
||||
const tab = commandTab?.id
|
||||
? commandTab
|
||||
: (await chrome.tabs.query({ active: true, currentWindow: true }))[0];
|
||||
if (!tab?.id) throw new Error("No active browser tab");
|
||||
if (!/^https?:\/\//i.test(tab.url || "")) {
|
||||
throw new Error("This browser page cannot be clipped");
|
||||
}
|
||||
|
||||
await chrome.scripting.executeScript({
|
||||
target: { tabId: tab.id },
|
||||
files: ["Readability.js", "Turndown.js"],
|
||||
});
|
||||
const results = await chrome.scripting.executeScript({
|
||||
target: { tabId: tab.id },
|
||||
func: extractReadablePage,
|
||||
});
|
||||
const extracted = results?.[0]?.result;
|
||||
let content = extracted?.content || "";
|
||||
if (!content) {
|
||||
const fallback = await chrome.scripting.executeScript({
|
||||
target: { tabId: tab.id },
|
||||
func: extractFallbackPage,
|
||||
});
|
||||
content = fallback?.[0]?.result || "";
|
||||
}
|
||||
if (!content.trim()) throw new Error(extracted?.error || "Failed to extract page content");
|
||||
content = limitExtractedContent(content);
|
||||
|
||||
return {
|
||||
title: extracted?.title || tab.title || "Untitled",
|
||||
url: tab.url || "",
|
||||
content,
|
||||
excerpt: extracted?.excerpt || "",
|
||||
};
|
||||
}
|
||||
|
||||
async function loadProjects(connection) {
|
||||
const { response, baseUrl } = await clipFetch("/projects", { method: "GET" }, connection);
|
||||
if (response.status === 401) throw new Error("Access token required or invalid");
|
||||
const data = await response.json();
|
||||
if (!response.ok || !data.ok) throw new Error(data.error || "Failed to load projects");
|
||||
return { projects: data.projects || [], baseUrl };
|
||||
}
|
||||
|
||||
function selectProject(projects, preferredProjectPath) {
|
||||
return projects.find((project) => project.path === preferredProjectPath)
|
||||
|| projects.find((project) => project.current)
|
||||
|| projects[0]
|
||||
|| null;
|
||||
}
|
||||
|
||||
async function submitClip(page, projectPath, connection) {
|
||||
const { response, baseUrl } = await clipFetch("/clip", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
title: page.title,
|
||||
url: page.url,
|
||||
content: page.content,
|
||||
projectPath,
|
||||
}),
|
||||
}, connection);
|
||||
const data = await response.json();
|
||||
if (!response.ok || !data.ok) throw new Error(data.error || `Clip failed: HTTP ${response.status}`);
|
||||
return { data, baseUrl };
|
||||
}
|
||||
|
||||
global.LLMWikiClipper = Object.freeze({
|
||||
DEFAULT_API_URLS,
|
||||
MAX_EXTRACTED_CONTENT_CHARS,
|
||||
normalizeServerUrl,
|
||||
loadSettings,
|
||||
clipFetch,
|
||||
extractActiveTab,
|
||||
loadProjects,
|
||||
selectProject,
|
||||
submitClip,
|
||||
});
|
||||
})(globalThis);
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 5.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 433 B |
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"manifest_version": 3,
|
||||
"name": "LLM Wiki Clipper",
|
||||
"version": "0.1.0",
|
||||
"description": "Clip web pages to your LLM Wiki knowledge base",
|
||||
"permissions": ["activeTab", "scripting", "storage"],
|
||||
"host_permissions": [
|
||||
"http://127.0.0.1:19827/*",
|
||||
"http://localhost:19827/*"
|
||||
],
|
||||
"optional_host_permissions": [
|
||||
"http://*/*",
|
||||
"https://*/*"
|
||||
],
|
||||
"action": {
|
||||
"default_popup": "popup.html",
|
||||
"default_icon": {
|
||||
"16": "icon16.png",
|
||||
"48": "icon48.png",
|
||||
"128": "icon128.png"
|
||||
}
|
||||
},
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
},
|
||||
"commands": {
|
||||
"clip-current-page": {
|
||||
"suggested_key": {
|
||||
"default": "Alt+Shift+L",
|
||||
"mac": "Command+Shift+L"
|
||||
},
|
||||
"description": "Clip the current page to LLM Wiki"
|
||||
}
|
||||
},
|
||||
"icons": {
|
||||
"16": "icon16.png",
|
||||
"48": "icon48.png",
|
||||
"128": "icon128.png"
|
||||
},
|
||||
"web_accessible_resources": [
|
||||
{
|
||||
"resources": ["Readability.js", "Turndown.js"],
|
||||
"matches": ["<all_urls>"]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
html, body {
|
||||
width: 480px;
|
||||
height: 500px;
|
||||
max-height: 500px;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
font-size: 13px;
|
||||
color: #1a1a2e;
|
||||
background: #fafafa;
|
||||
overflow: hidden !important;
|
||||
}
|
||||
.header {
|
||||
padding: 12px 16px;
|
||||
background: #1a1a2e;
|
||||
color: white;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.header h1 { font-size: 14px; font-weight: 600; }
|
||||
.header .icon { font-size: 18px; }
|
||||
.content { padding: 12px 16px; }
|
||||
.status {
|
||||
padding: 8px 12px;
|
||||
border-radius: 6px;
|
||||
font-size: 12px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.status.connected { background: #ecfdf5; color: #065f46; }
|
||||
.status.disconnected { background: #fef2f2; color: #991b1b; }
|
||||
.status.sending { background: #eff6ff; color: #1e40af; }
|
||||
.status.success { background: #ecfdf5; color: #065f46; }
|
||||
.status.error { background: #fef2f2; color: #991b1b; }
|
||||
.field { margin-bottom: 8px; }
|
||||
.field label {
|
||||
display: block;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: #6b7280;
|
||||
text-transform: uppercase;
|
||||
margin-bottom: 3px;
|
||||
}
|
||||
.field input, .field select {
|
||||
width: 100%;
|
||||
padding: 6px 8px;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
font-size: 13px;
|
||||
font-family: inherit;
|
||||
outline: none;
|
||||
background: white;
|
||||
}
|
||||
.field input:focus, .field select:focus {
|
||||
border-color: #6366f1;
|
||||
box-shadow: 0 0 0 2px rgba(99,102,241,0.1);
|
||||
}
|
||||
.field select { cursor: pointer; }
|
||||
.connection-settings {
|
||||
margin-bottom: 10px;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
background: white;
|
||||
}
|
||||
.connection-settings summary {
|
||||
padding: 7px 10px;
|
||||
cursor: pointer;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: #4b5563;
|
||||
}
|
||||
.connection-settings-body { padding: 0 10px 9px; }
|
||||
.btn-secondary { margin-top: 2px; padding: 7px; background: #e5e7eb; color: #374151; }
|
||||
.btn-secondary:hover { background: #d1d5db; }
|
||||
.url-preview {
|
||||
padding: 4px 8px;
|
||||
background: #f3f4f6;
|
||||
border-radius: 4px;
|
||||
font-size: 11px;
|
||||
color: #6b7280;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
#contentPreview {
|
||||
padding: 10px;
|
||||
background: #f3f4f6;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
font-size: 12px;
|
||||
color: #374151;
|
||||
line-height: 1.6;
|
||||
max-height: 260px;
|
||||
overflow-y: auto;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
#contentPreview::-webkit-scrollbar { width: 8px; }
|
||||
#contentPreview::-webkit-scrollbar-track { background: #f3f4f6; border-radius: 0 6px 6px 0; }
|
||||
#contentPreview::-webkit-scrollbar-thumb { background: #c5c5c5; border-radius: 4px; }
|
||||
#contentPreview::-webkit-scrollbar-thumb:hover { background: #999; }
|
||||
.btn {
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
margin-top: 10px;
|
||||
}
|
||||
.btn-primary { background: #4f46e5; color: white; }
|
||||
.btn-primary:hover { background: #4338ca; }
|
||||
.btn-primary:disabled { background: #9ca3af; cursor: not-allowed; }
|
||||
.footer {
|
||||
padding: 6px 16px;
|
||||
border-top: 1px solid #e5e7eb;
|
||||
font-size: 10px;
|
||||
color: #9ca3af;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<span class="icon">📚</span>
|
||||
<h1>LLM Wiki Clipper</h1>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<div id="statusBar" class="status disconnected">Checking connection...</div>
|
||||
|
||||
<details class="connection-settings" id="connectionSettings">
|
||||
<summary>Connection settings</summary>
|
||||
<div class="connection-settings-body">
|
||||
<div class="field">
|
||||
<label for="serverUrlInput">Server address</label>
|
||||
<input type="text" id="serverUrlInput" placeholder="http://192.168.1.50:19827">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="accessTokenInput">Access token</label>
|
||||
<input type="password" id="accessTokenInput" placeholder="Required for LAN access">
|
||||
</div>
|
||||
<button class="btn btn-secondary" id="saveConnectionBtn" type="button">Save and reconnect</button>
|
||||
</div>
|
||||
</details>
|
||||
|
||||
<div class="field">
|
||||
<label>Save to Project</label>
|
||||
<select id="projectSelect">
|
||||
<option value="">Loading projects...</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>Title</label>
|
||||
<input type="text" id="titleInput" placeholder="Page title">
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>URL</label>
|
||||
<div class="url-preview" id="urlPreview">—</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>Content Preview</label>
|
||||
<div id="contentPreview">Extracting content...</div>
|
||||
</div>
|
||||
|
||||
<button class="btn btn-primary" id="clipBtn" disabled>
|
||||
📎 Clip to Wiki
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<div>Content will be saved and auto-ingested into your wiki</div>
|
||||
<div id="shortcutHint" style="margin-top: 2px; color: #6b7280;">Loading shortcut...</div>
|
||||
</div>
|
||||
|
||||
<script src="clipper-core.js"></script>
|
||||
<script src="popup.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,205 @@
|
||||
const clipperCore = globalThis.LLMWikiClipper;
|
||||
|
||||
const statusBar = document.getElementById("statusBar");
|
||||
const titleInput = document.getElementById("titleInput");
|
||||
const urlPreview = document.getElementById("urlPreview");
|
||||
const contentPreview = document.getElementById("contentPreview");
|
||||
const clipBtn = document.getElementById("clipBtn");
|
||||
const projectSelect = document.getElementById("projectSelect");
|
||||
const serverUrlInput = document.getElementById("serverUrlInput");
|
||||
const accessTokenInput = document.getElementById("accessTokenInput");
|
||||
const saveConnectionBtn = document.getElementById("saveConnectionBtn");
|
||||
const connectionSettings = document.getElementById("connectionSettings");
|
||||
const shortcutHint = document.getElementById("shortcutHint");
|
||||
|
||||
let extractedContent = "";
|
||||
let pageUrl = "";
|
||||
let apiUrl = clipperCore.DEFAULT_API_URLS[0];
|
||||
let accessToken = "";
|
||||
|
||||
async function loadConnectionSettings() {
|
||||
const saved = await clipperCore.loadSettings();
|
||||
apiUrl = saved.serverUrl;
|
||||
accessToken = saved.accessToken;
|
||||
serverUrlInput.value = apiUrl;
|
||||
accessTokenInput.value = accessToken;
|
||||
}
|
||||
|
||||
async function clipFetch(path, options) {
|
||||
const result = await clipperCore.clipFetch(path, options, {
|
||||
serverUrl: apiUrl,
|
||||
accessToken,
|
||||
});
|
||||
apiUrl = result.baseUrl;
|
||||
return result.response;
|
||||
}
|
||||
|
||||
async function checkConnection() {
|
||||
let connectionError = "";
|
||||
try {
|
||||
const res = await clipFetch("/status", { method: "GET" });
|
||||
const data = await res.json();
|
||||
if (res.status === 401) throw new Error("Access token required or invalid");
|
||||
if (data.ok) {
|
||||
statusBar.className = "status connected";
|
||||
statusBar.textContent = "✓ Connected to LLM Wiki";
|
||||
await loadProjects();
|
||||
return true;
|
||||
}
|
||||
} catch (err) {
|
||||
connectionError = err?.message || "";
|
||||
}
|
||||
statusBar.className = "status disconnected";
|
||||
statusBar.textContent = connectionError.includes("token")
|
||||
? "✗ Access token required or invalid"
|
||||
: "✗ Cannot connect to LLM Wiki"
|
||||
statusBar.title = connectionError;
|
||||
clipBtn.disabled = true;
|
||||
projectSelect.innerHTML = '<option value="">App not running</option>';
|
||||
return false;
|
||||
}
|
||||
|
||||
async function loadProjects() {
|
||||
try {
|
||||
const res = await clipFetch("/projects", { method: "GET" });
|
||||
const data = await res.json();
|
||||
if (data.ok && data.projects?.length > 0) {
|
||||
const { preferredProjectPath } = await clipperCore.loadSettings();
|
||||
projectSelect.innerHTML = "";
|
||||
for (const proj of data.projects) {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = proj.path;
|
||||
opt.textContent = proj.name + (proj.current ? " (current)" : "");
|
||||
if (proj.path === preferredProjectPath || (!preferredProjectPath && proj.current)) {
|
||||
opt.selected = true;
|
||||
}
|
||||
projectSelect.appendChild(opt);
|
||||
}
|
||||
if (!projectSelect.value && data.projects[0]) {
|
||||
projectSelect.value = data.projects[0].path;
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch {}
|
||||
// Fallback to current project
|
||||
try {
|
||||
const res = await clipFetch("/project", { method: "GET" });
|
||||
const data = await res.json();
|
||||
if (data.ok && data.path) {
|
||||
const name = data.path.replace(/\\/g, "/").split("/").pop() || data.path;
|
||||
projectSelect.innerHTML = `<option value="${data.path}">${name}</option>`;
|
||||
}
|
||||
} catch {
|
||||
projectSelect.innerHTML = '<option value="">No projects</option>';
|
||||
}
|
||||
}
|
||||
|
||||
async function extractContent() {
|
||||
try {
|
||||
const page = await clipperCore.extractActiveTab();
|
||||
pageUrl = page.url;
|
||||
titleInput.value = page.title;
|
||||
urlPreview.textContent = pageUrl;
|
||||
extractedContent = page.content;
|
||||
contentPreview.textContent = page.excerpt
|
||||
? `📝 ${page.excerpt}\n\n---\n\n${extractedContent}`
|
||||
: extractedContent;
|
||||
clipBtn.disabled = false;
|
||||
} catch (err) {
|
||||
contentPreview.textContent = `Error: ${err.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
async function sendClip() {
|
||||
const selectedProject = projectSelect.value;
|
||||
if (!selectedProject) {
|
||||
statusBar.className = "status error";
|
||||
statusBar.textContent = "✗ Please select a project";
|
||||
return;
|
||||
}
|
||||
|
||||
clipBtn.disabled = true;
|
||||
statusBar.className = "status sending";
|
||||
statusBar.textContent = "⏳ Sending to LLM Wiki...";
|
||||
|
||||
try {
|
||||
const result = await clipperCore.submitClip({
|
||||
title: titleInput.value,
|
||||
url: pageUrl,
|
||||
content: extractedContent,
|
||||
}, selectedProject, {
|
||||
serverUrl: apiUrl,
|
||||
accessToken,
|
||||
});
|
||||
apiUrl = result.baseUrl;
|
||||
await chrome.storage.local.set({
|
||||
serverUrl: apiUrl,
|
||||
preferredProjectPath: selectedProject,
|
||||
});
|
||||
const projectName = projectSelect.options[projectSelect.selectedIndex]?.textContent || "project";
|
||||
statusBar.className = "status success";
|
||||
statusBar.textContent = `✓ Saved to ${projectName}`;
|
||||
clipBtn.textContent = "✓ Clipped!";
|
||||
} catch (err) {
|
||||
statusBar.className = "status error";
|
||||
statusBar.textContent = `✗ Connection failed: ${err.message}`;
|
||||
clipBtn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
clipBtn.addEventListener("click", sendClip);
|
||||
|
||||
projectSelect.addEventListener("change", () => {
|
||||
if (projectSelect.value) {
|
||||
void chrome.storage.local.set({ preferredProjectPath: projectSelect.value });
|
||||
}
|
||||
});
|
||||
|
||||
saveConnectionBtn.addEventListener("click", async () => {
|
||||
try {
|
||||
const nextUrl = clipperCore.normalizeServerUrl(serverUrlInput.value);
|
||||
const originPattern = `${new URL(nextUrl).origin}/*`;
|
||||
const granted = await chrome.permissions.request({ origins: [originPattern] });
|
||||
if (!granted) throw new Error("Host permission was not granted");
|
||||
apiUrl = nextUrl;
|
||||
accessToken = accessTokenInput.value.trim();
|
||||
await chrome.storage.local.set({ serverUrl: apiUrl, accessToken });
|
||||
connectionSettings.open = false;
|
||||
clipBtn.disabled = true;
|
||||
await checkConnection();
|
||||
} catch (err) {
|
||||
connectionSettings.open = true;
|
||||
statusBar.className = "status error";
|
||||
statusBar.textContent = `✗ ${err.message}`;
|
||||
}
|
||||
});
|
||||
|
||||
// Resize content preview to fill available space without causing popup scroll
|
||||
function resizePreview() {
|
||||
const totalHeight = 500; // matches html/body height
|
||||
const preview = document.getElementById("contentPreview");
|
||||
if (!preview) return;
|
||||
|
||||
// Calculate space used by everything except the preview
|
||||
const previewRect = preview.getBoundingClientRect();
|
||||
const bottomSpace = totalHeight - previewRect.top - 60; // 60px for button + footer
|
||||
const maxH = Math.max(100, Math.min(300, bottomSpace));
|
||||
preview.style.maxHeight = maxH + "px";
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const commands = await chrome.commands.getAll();
|
||||
const clipCommand = commands.find((command) => command.name === "clip-current-page");
|
||||
shortcutHint.textContent = clipCommand?.shortcut
|
||||
? `Shortcut: ${clipCommand.shortcut}`
|
||||
: "Set a shortcut at chrome://extensions/shortcuts";
|
||||
await loadConnectionSettings();
|
||||
const connected = await checkConnection();
|
||||
// Always extract content so user can preview, even if app not running
|
||||
await extractContent();
|
||||
if (!connected) {
|
||||
clipBtn.disabled = true;
|
||||
clipBtn.textContent = "📎 App not running — cannot save";
|
||||
}
|
||||
setTimeout(resizePreview, 100);
|
||||
})();
|
||||
Reference in New Issue
Block a user