-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: started markdown export feature
- Loading branch information
Showing
10 changed files
with
260 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
margin: 0; | ||
padding: 0; | ||
font-family: 'Roboto', sans-serif; | ||
background-color: white; | ||
} | ||
|
||
pre { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { DataItem, SearchResult, WorkSpace } from '../types'; | ||
import { DataInterface } from '../datasource/DataInterface'; | ||
import { DataSourceRegistry } from '../datasource/DataSourceRegistry'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { isCollectionItem, isMediaItem, isNoteItem } from '../utils'; | ||
import rimraf from 'rimraf'; | ||
import { InternalTag } from '../datasource/InternalTag'; | ||
import { EditorRegistry } from '../editors/EditorRegistry'; | ||
|
||
export type MarkdownExportOptions = { | ||
targetFolder: string; | ||
makeTextFilesToMarkdown: boolean; | ||
} | ||
|
||
export const runMarkdownExport = async (options: MarkdownExportOptions, workspace: WorkSpace) => { | ||
console.log("Clearing...", options.targetFolder); | ||
await new Promise(r => rimraf(options.targetFolder, r)); | ||
await fs.ensureDir(options.targetFolder); | ||
|
||
const di = new DataInterface( | ||
DataSourceRegistry.getDataSource(workspace), | ||
EditorRegistry.Instance, | ||
300 | ||
); | ||
|
||
const items: DataItem[] = []; | ||
let result: SearchResult = { nextPageAvailable: true, results: [] }; | ||
do { | ||
result = await di.search({ all: true, limit: 200, pagingValue: result.nextPagingValue }); | ||
for (const item of result.results) { | ||
items.push(item); | ||
if (isMediaItem(item) && !item.referencePath) { | ||
const mediaItem = await di.loadMediaItemContentAsPath(item.id); | ||
console.log("media item", item, mediaItem); | ||
} else if (isNoteItem(item)) { | ||
const content = await di.getNoteItemContent(item.id); | ||
console.log("note", item); | ||
} | ||
} | ||
} while (result.nextPageAvailable); | ||
|
||
const rootItem = items.find(item => item.tags.includes(InternalTag.WorkspaceRoot)); | ||
if (!rootItem) { | ||
throw new Error("Root item not found"); | ||
} | ||
|
||
await processItem(rootItem, "", options, di, items); | ||
|
||
|
||
await di.unload(); | ||
} | ||
|
||
const processItem = async (item: DataItem, parent: string, options: MarkdownExportOptions, di: DataInterface, items: DataItem[]) => { | ||
console.log(`Exporting ${item.name} (${parent})...`); | ||
const pathFriendlyName = item.name.replace(/[^a-zA-Z0-9- ]/g, "_"); | ||
const itemPath = path.join(options.targetFolder, parent); | ||
|
||
if (isNoteItem(item)) { | ||
const content = await di.getNoteItemContent<any>(item.id); | ||
const dump = await EditorRegistry.Instance.getEditorWithId(item.noteType).exportContent(content, item, options); | ||
const ext = options.makeTextFilesToMarkdown ? "md" : EditorRegistry.Instance.getEditorWithId(item.noteType).getExportFileExtension(content); | ||
await fs.ensureDir(itemPath); | ||
await fs.writeFile(path.join(itemPath, `${pathFriendlyName}.${ext}`), dump); | ||
} | ||
|
||
if (item.childIds.length > 0 && isCollectionItem(item)) { | ||
const newParent = path.join(parent, pathFriendlyName); | ||
await fs.ensureDir(path.join(itemPath, pathFriendlyName)); | ||
|
||
for (const child of item.childIds) { | ||
const childItem = items.find(i => i.id === child); | ||
if (childItem) { | ||
await processItem(childItem, newParent, options, di, items); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { EditorDefinition } from '../EditorDefinition'; | ||
import { EditorComponent } from './EditorComponent'; | ||
import { SmallPreviewComponent } from './SmallPreviewComponent'; | ||
import { ReactRenderer, renderDocument, ReactSerializer, TextSerializer } from '@atlaskit/renderer'; | ||
import ReactDOM from 'react-dom'; | ||
import Turndown from 'turndown'; | ||
import * as React from 'react'; | ||
import { NoteDataItem } from '../../types'; | ||
|
||
export interface AtlassianNoteEditorContent { | ||
adf: any; | ||
} | ||
|
||
export class AtlaskitNoteEditor implements EditorDefinition<'atlaskit-editor-note', AtlassianNoteEditorContent> { | ||
public id = 'atlaskit-editor-note' as const; | ||
public name = 'Note'; | ||
public editorComponent = EditorComponent; | ||
public smallPreviewComponent = SmallPreviewComponent; | ||
public canInsertFiles = false; | ||
|
||
public initializeContent(): AtlassianNoteEditorContent { | ||
return { | ||
adf: { | ||
version: 1, | ||
type: 'doc', | ||
content: [], | ||
}, | ||
}; | ||
} | ||
|
||
public async exportContent(content: AtlassianNoteEditorContent, note: NoteDataItem<any>): Promise<string> { | ||
const dumpElement = document.createElement('div'); | ||
dumpElement.style.display = 'none'; | ||
document.body.appendChild(dumpElement); | ||
return new Promise<string>(r => { | ||
ReactDOM.render(<ReactRenderer document={content.adf} />, dumpElement, () => { | ||
setTimeout(() => { | ||
// patch p elements from inside li elements | ||
Array.from(dumpElement.getElementsByTagName("li")).forEach(li => { | ||
if (li.children.length === 1 && li.children.item(0).tagName.toLowerCase() === "p") { | ||
li.innerHTML = li.children[0].innerHTML; | ||
} | ||
}); | ||
// patch line numbers away from code blocks | ||
Array.from(dumpElement.getElementsByClassName("linenumber")).forEach(item => { | ||
item.outerHTML = ""; | ||
}); | ||
Array.from(dumpElement.querySelectorAll<HTMLElement>(".code-block code > span")).forEach(item => { | ||
item.outerHTML = item.innerHTML; | ||
}); | ||
Array.from(dumpElement.querySelectorAll<HTMLElement>(".code-block")).forEach(item => { | ||
|
||
const code = item.querySelector<HTMLElement>("code").innerHTML; | ||
item.innerHTML = `<pre><code>${code}</code></pre>`; | ||
}); | ||
|
||
|
||
const text = dumpElement.innerHTML; | ||
document.body.removeChild(dumpElement); | ||
const td = new Turndown({ | ||
headingStyle: "atx", | ||
hr: "---", | ||
codeBlockStyle: "fenced", | ||
}); | ||
const markdown = td.turndown(text); | ||
const tags = note.tags.map(tag => `#${tag}`).join(' '); | ||
r(tags ? `${markdown}\n\n${tags}` : markdown); | ||
}); | ||
}); | ||
}) | ||
} | ||
|
||
public getExportFileExtension(content: AtlassianNoteEditorContent): string { | ||
return 'md'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3106,6 +3106,11 @@ | |
resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.6.tgz#a9ca4b70a18b270ccb2bc0aaafefd1d486b7ea74" | ||
integrity sha512-W+bw9ds02rAQaMvaLYxAbJ6cvguW/iJXNT6lTssS1ps6QdrMKttqEAMEG/b5CR8TZl3/L7/lH0ZV5nNR1LXikA== | ||
|
||
"@types/turndown@^5.0.1": | ||
version "5.0.1" | ||
resolved "https://registry.yarnpkg.com/@types/turndown/-/turndown-5.0.1.tgz#fcda7b02cda4c9d445be1440036df20f335b9387" | ||
integrity sha512-N8Ad4e3oJxh9n9BiZx9cbe/0M3kqDpOTm2wzj13wdDUxDPjfjloWIJaquZzWE1cYTAHpjOH3rcTnXQdpEfS/SQ== | ||
|
||
"@types/uglify-js@*": | ||
version "3.9.3" | ||
resolved "https://registry.yarnpkg.com/@types/uglify-js/-/uglify-js-3.9.3.tgz#d94ed608e295bc5424c9600e6b8565407b6b4b6b" | ||
|
@@ -5511,6 +5516,11 @@ domhandler@^2.3.0: | |
dependencies: | ||
domelementtype "1" | ||
|
||
domino@^2.1.6: | ||
version "2.1.6" | ||
resolved "https://registry.yarnpkg.com/domino/-/domino-2.1.6.tgz#fe4ace4310526e5e7b9d12c7de01b7f485a57ffe" | ||
integrity sha512-3VdM/SXBZX2omc9JF9nOPCtDaYQ67BGp5CoLpIQlO2KCAPETs8TcDHacF26jXadGbvUteZzRTeos2fhID5+ucQ== | ||
|
||
[email protected]: | ||
version "1.5.1" | ||
resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.5.1.tgz#dcd8488a26f563d61079e48c9f7b7e32373682cf" | ||
|
@@ -11758,6 +11768,13 @@ tunnel@^0.0.6: | |
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c" | ||
integrity sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg== | ||
|
||
turndown@^7.1.2: | ||
version "7.1.2" | ||
resolved "https://registry.yarnpkg.com/turndown/-/turndown-7.1.2.tgz#7feb838c78f14241e79ed92a416e0d213e044a29" | ||
integrity sha512-ntI9R7fcUKjqBP6QU8rBK2Ehyt8LAzt3UBT9JR9tgo6GtuKvyUzpayWmeMKJw1DPdXzktvtIT8m2mVXz+bL/Qg== | ||
dependencies: | ||
domino "^2.1.6" | ||
|
||
tweetnacl@^0.14.3, tweetnacl@~0.14.0: | ||
version "0.14.5" | ||
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64" | ||
|