Skip to content
Open
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
4 changes: 2 additions & 2 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
</header>
<div id="body">
<div id="content">
<input type="file" name="file-select" id="file-select" accept=".xmindmark,.md,.txt" style="display: none;" />
<label id="file-select-handler" for="file-select">Choose a <code>.xmindmark</code> / <code>.md</code> / <code>.txt</code> file</label>
<input type="file" name="file-select" id="file-select" accept=".xmind,.xmindmark,.md,.txt" style="display: none;" />
<label id="file-select-handler" for="file-select">Choose a <code>.xmind</code> / <code>.xmindmark</code> / <code>.md</code> / <code>.txt</code> file</label>
<div>Or directly edit your minds below:</div>
<div id="input"></div>
</div>
Expand Down
19 changes: 17 additions & 2 deletions playground/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { parseXMindToXMindMarkFile } from '../../src'

export async function loadFileAsText(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const fileReader = new FileReader()
fileReader.addEventListener('loadend', () => resolve(fileReader.result as string))
fileReader.addEventListener('error', reject)
fileReader.addEventListener('abort', reject)
fileReader.readAsText(file)
const suffix = file.name.split('.').slice(-1)[0]
if (suffix === 'xmind') {
fileReader.readAsArrayBuffer(file)
fileReader.addEventListener('loadend', async () => {
const content = await parseXMindToXMindMarkFile(fileReader.result as ArrayBuffer);
if (content) {
resolve(content)
} else {
reject(new Error('Not valid .xmind file.'))
}
})
} else {
fileReader.readAsText(file)
fileReader.addEventListener('loadend', () => resolve(fileReader.result as string))
}
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const resetCommand = new Command('reset')

export async function fromActionHandler(xmindFilePath: string, outputFilePath: string | undefined) {
const xmindFile = readFileSync(xmindFilePath)
const xmindmarkContent = await parseXMindToXMindMarkFile(xmindFile)
const xmindmarkContent = await parseXMindToXMindMarkFile(xmindFile.buffer as ArrayBuffer);

if (outputFilePath) {
let outputPath = resolve(outputFilePath)
Expand All @@ -44,7 +44,7 @@ export const fromCommand = new Command('from')
.description('Generate .xmindmark file from other types of file')
.action(fromActionHandler)

export async function mainActionHandler(files: string[], options: CLIOptions, command: Command) {
export async function mainActionHandler(files: string[], options: CLIOptions, command: Command) {
if (files.length === 0) command.help()

const { format, outputDir } = options
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { parseXMindMarkToXMindFile } from './lib/xmindmark-to-xmind'
export { parseXMindToXMindMarkFile } from './lib/xmind-to-xmindmark'
7 changes: 4 additions & 3 deletions src/lib/xmind-to-xmindmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ function isOrderInsideRange(range: ClosedRange, order: number): boolean {
}

function makeIndentOfLine({ depth }: Pick<TopicScope, 'depth'>): string {
return Array.from({ length: depth }).reduce<string>(prevIndent => prevIndent.concat(' '), '')
return depth <= 1 ?
'' : Array.from({ length: depth - 1 }).reduce<string>(prevIndent => prevIndent.concat(' '), '')
}

function makePrefixOfLine({ depth, type }: TopicScope): string {
Expand Down Expand Up @@ -225,7 +226,7 @@ function makeIdentifyBoundaries(topic: TopicModel): Boundary[] {

if (topic.boundaries.length === 1) return [{ ...topic.boundaries[0], identifier: 'B' }]

return topic.boundaries.map((boundary, i) => ({ ...boundary, identifier: `B${i + 1}`}))
return topic.boundaries.map((boundary, i) => ({ ...boundary, identifier: `B${i + 1}` }))
}

function makeIdentifySummaries(topic: TopicModel): Summary[] {
Expand All @@ -237,7 +238,7 @@ function makeIdentifySummaries(topic: TopicModel): Summary[] {
&& topic.children.summary.length === topic.summaries.length
) {
const summaries = topic.summaries.map((summary, i) => {
const title = topic.children!.summary?.find(({ id }) => id === summary.topicId )?.title
const title = topic.children!.summary?.find(({ id }) => id === summary.topicId)?.title

return typeof title !== 'undefined'
? { ...summary, title, identifier: `S${i + 1}` }
Expand Down
56 changes: 28 additions & 28 deletions src/tests/xmind-to-xmindmark.sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,32 +511,32 @@ export const inputJSON = [

export const expectedOutputXMindMark =
`Central Topic
- Main Topic 1 [^1]
- Subtopic 1 [2]
- Subtopic 1 [B1]
- Subtopic 2 [B1][B2]
- Subtopic 3 [B2]
[B1]: title1
[B2]: title2
- Subtopic 2
- Subtopic 3 [^2]
- Subtopic 1 [B]
- Subtopic 2 [B]
- Subtopic 3 [B]
[B]: title2
- Main Topic 2 [1]
- Main Topic 3
- Subtopic 1
- Subtopic 1 [^3][S1]
- Subtopic 2 [B][S1][S2]
- Subtopic 3 [B][S1][S2]
- Subtopic 4 [S2]
[S1]: Summary 1
- Subtopic 1
- Subtopic 2
- Subtopic 3
[S2]: Summary 2
- Subtopic 2
- Subtopic 3
- Main Topic 4 [3]
- Main Topic 1 [^1]
- Subtopic 1 [2]
- Subtopic 1 [B1]
- Subtopic 2 [B1][B2]
- Subtopic 3 [B2]
[B1]: title1
[B2]: title2
- Subtopic 2
- Subtopic 3 [^2]
- Subtopic 1 [B]
- Subtopic 2 [B]
- Subtopic 3 [B]
[B]: title2
- Main Topic 2 [1]
- Main Topic 3
- Subtopic 1
- Subtopic 1 [^3][S1]
- Subtopic 2 [B][S1][S2]
- Subtopic 3 [B][S1][S2]
- Subtopic 4 [S2]
[S1]: Summary 1
- Subtopic 1
- Subtopic 2
- Subtopic 3
[S2]: Summary 2
- Subtopic 2
- Subtopic 3
- Main Topic 4 [3]
`