mirror of
https://github.com/docmost/docmost.git
synced 2026-08-24 07:12:38 +10:00
markdown export
This commit is contained in:
@@ -32,9 +32,8 @@ export const tabsExtension = {
|
||||
}> = [];
|
||||
|
||||
HEADER_RE.lastIndex = 0;
|
||||
let m: RegExpExecArray | null = null;
|
||||
|
||||
while ((m = HEADER_RE.exec(src)) !== null) {
|
||||
for (let m = HEADER_RE.exec(src); m !== null; m = HEADER_RE.exec(src)) {
|
||||
headers.push({
|
||||
index: m.index,
|
||||
raw: m[0],
|
||||
@@ -47,6 +46,7 @@ export const tabsExtension = {
|
||||
|
||||
const tabs: MarkdownTab[] = [];
|
||||
let consumed = 0;
|
||||
let breakOutOfTabSet = false;
|
||||
|
||||
for (let i = 0; i < headers.length; i++) {
|
||||
const h = headers[i];
|
||||
@@ -56,15 +56,38 @@ export const tabsExtension = {
|
||||
const headerEnd = h.index + h.raw.length;
|
||||
const bodyStart =
|
||||
src.charCodeAt(headerEnd) === 10 ? headerEnd + 1 : headerEnd;
|
||||
|
||||
const next = headers[i + 1];
|
||||
const bodyEnd = next ? next.index : src.length;
|
||||
const bodyLimit = next ? next.index : src.length;
|
||||
let bodyEnd = bodyLimit;
|
||||
let lineStart = bodyStart;
|
||||
|
||||
while (lineStart < bodyLimit) {
|
||||
const newlineIndex = src.indexOf('\n', lineStart);
|
||||
const lineEnd =
|
||||
newlineIndex === -1 || newlineIndex > bodyLimit
|
||||
? bodyLimit
|
||||
: newlineIndex;
|
||||
|
||||
const line = src.slice(lineStart, lineEnd);
|
||||
const isBlank = line.trim() === '';
|
||||
const isIndented = /^( {2,4}|\t)/.test(line);
|
||||
|
||||
if (!isBlank && !isIndented) {
|
||||
bodyEnd = lineStart;
|
||||
breakOutOfTabSet = true;
|
||||
break;
|
||||
}
|
||||
|
||||
lineStart = lineEnd < bodyLimit ? lineEnd + 1 : bodyLimit;
|
||||
}
|
||||
|
||||
let body = src.slice(bodyStart, bodyEnd).replace(/\n+$/, '');
|
||||
|
||||
if (body.length > 0) {
|
||||
body = body
|
||||
.split('\n')
|
||||
.map((line) => line.replace(/^\s{0,4}/, ''))
|
||||
.map((line) => line.replace(/^\s{2,4}/, ''))
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
@@ -75,6 +98,8 @@ export const tabsExtension = {
|
||||
});
|
||||
|
||||
consumed = bodyEnd;
|
||||
|
||||
if (breakOutOfTabSet) break;
|
||||
}
|
||||
|
||||
if (tabs.length < 1) return;
|
||||
|
||||
@@ -39,6 +39,24 @@ export function htmlToMarkdown(html: string): string {
|
||||
return turndownService.turndown(html).replaceAll('<br>', ' ');
|
||||
}
|
||||
|
||||
const hasPreviousTabs = (node: HTMLElement) => {
|
||||
// we want to make this as cheap as reasonable since it
|
||||
// would be preferable to return a false positive
|
||||
// than to make the editor noticably slower
|
||||
let el = node.previousElementSibling;
|
||||
let checks = 0;
|
||||
|
||||
while (el) {
|
||||
if (el.getAttribute('data-type') === 'tabs') return true;
|
||||
el = el.previousElementSibling;
|
||||
checks += 1;
|
||||
|
||||
if (checks === 100) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
function tabs(turndownService: _TurndownService) {
|
||||
turndownService.addRule('tabs', {
|
||||
filter: (node: HTMLInputElement) =>
|
||||
@@ -49,7 +67,10 @@ function tabs(turndownService: _TurndownService) {
|
||||
);
|
||||
if (tabNodes.length === 0) return content;
|
||||
|
||||
const tabBlocks = tabNodes.map((tabNode) => {
|
||||
const isNestedTabsNode =
|
||||
node.parentElement?.closest('div[data-type="tabs"]') !== null;
|
||||
|
||||
const tabBlocks = tabNodes.map((tabNode, index) => {
|
||||
const labelNode = tabNode.querySelector(
|
||||
':scope > div[data-type="tabLabel"]',
|
||||
);
|
||||
@@ -62,7 +83,13 @@ function tabs(turndownService: _TurndownService) {
|
||||
? turndownService.turndown(panelNode.innerHTML).trim()
|
||||
: '';
|
||||
|
||||
return `=== "${label}"\n${indentMarkdownBlock(panelMarkdown)}`;
|
||||
const isFirstTabInSet = index === 0;
|
||||
const marker =
|
||||
isFirstTabInSet && !isNestedTabsNode && hasPreviousTabs(node)
|
||||
? '!'
|
||||
: '';
|
||||
|
||||
return `===${marker} "${label}"\n${indentMarkdownBlock(panelMarkdown)}`;
|
||||
});
|
||||
|
||||
return `\n\n${tabBlocks.join('\n\n')}\n\n`;
|
||||
@@ -85,7 +112,9 @@ function listParagraph(turndownService: _TurndownService) {
|
||||
function orderedListItem(turndownService: _TurndownService) {
|
||||
turndownService.addRule('orderedListItem', {
|
||||
filter: function (node: HTMLInputElement) {
|
||||
return node.nodeName === 'LI' && node.getAttribute('data-type') !== 'taskItem';
|
||||
return (
|
||||
node.nodeName === 'LI' && node.getAttribute('data-type') !== 'taskItem'
|
||||
);
|
||||
},
|
||||
replacement: (content: string, node: HTMLInputElement, options: any) => {
|
||||
const parent = node.parentNode as HTMLElement;
|
||||
@@ -146,9 +175,7 @@ function taskList(turndownService: _TurndownService) {
|
||||
const prefix = `- ${isChecked ? '[x]' : '[ ]'} `;
|
||||
|
||||
return (
|
||||
prefix +
|
||||
text +
|
||||
(node.nextSibling && !/\n$/.test(text) ? '\n' : '')
|
||||
prefix + text + (node.nextSibling && !/\n$/.test(text) ? '\n' : '')
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -243,9 +270,7 @@ function video(turndownService: _TurndownService) {
|
||||
replacement: function (_content: string, node: HTMLInputElement) {
|
||||
const src = node.getAttribute('src') || '';
|
||||
const ariaLabel = node.getAttribute('aria-label');
|
||||
const name = sanitizeMdLinkText(
|
||||
ariaLabel || getBasename(src) || src,
|
||||
);
|
||||
const name = sanitizeMdLinkText(ariaLabel || getBasename(src) || src);
|
||||
return '[' + name + '](' + src + ')';
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user