-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate copy without wikilinks (#1365)
* add generate standalone note command * fix embeded wikilinks * refactor convertLinksFormat function & add 4 user command interfaces * change user interface * modify createUpdateLinkEdit to accomplish convert * only images can be embedded * keey filename when using in page anchor * give a default value to alias in link format combination branch * add tests to createUpdateLinkEdit about changint links' type and isEmbed * get target from getIdentifier --------- Co-authored-by: Riccardo <[email protected]>
- Loading branch information
1 parent
22b837f
commit cef8d2a
Showing
10 changed files
with
575 additions
and
9 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
71 changes: 71 additions & 0 deletions
71
packages/foam-vscode/src/core/janitor/convert-links-format.test.ts
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,71 @@ | ||
import { convertLinkFormat } from '.'; | ||
import { TEST_DATA_DIR } from '../../test/test-utils'; | ||
import { MarkdownResourceProvider } from '../services/markdown-provider'; | ||
import { Resource } from '../model/note'; | ||
import { FoamWorkspace } from '../model/workspace'; | ||
import { Logger } from '../utils/log'; | ||
import fs from 'fs'; | ||
import { URI } from '../model/uri'; | ||
import { createMarkdownParser } from '../services/markdown-parser'; | ||
import { FileDataStore } from '../../test/test-datastore'; | ||
|
||
Logger.setLevel('error'); | ||
|
||
describe('generateStdMdLink', () => { | ||
let _workspace: FoamWorkspace; | ||
// TODO slug must be reserved for actual slugs, not file names | ||
const findBySlug = (slug: string): Resource => { | ||
return _workspace | ||
.list() | ||
.find(res => res.uri.getName() === slug) as Resource; | ||
}; | ||
|
||
beforeAll(async () => { | ||
/** Use fs for reading files in units where vscode.workspace is unavailable */ | ||
const readFile = async (uri: URI) => | ||
(await fs.promises.readFile(uri.toFsPath())).toString(); | ||
const dataStore = new FileDataStore( | ||
readFile, | ||
TEST_DATA_DIR.joinPath('__scaffold__').toFsPath() | ||
); | ||
const parser = createMarkdownParser(); | ||
const mdProvider = new MarkdownResourceProvider(dataStore, parser); | ||
_workspace = await FoamWorkspace.fromProviders([mdProvider], dataStore); | ||
}); | ||
|
||
it('initialised test graph correctly', () => { | ||
expect(_workspace.list().length).toEqual(11); | ||
}); | ||
|
||
it('can generate markdown links correctly', async () => { | ||
const note = findBySlug('file-with-different-link-formats'); | ||
const actual = note.links | ||
.filter(link => link.type === 'wikilink') | ||
.map(link => convertLinkFormat(link, 'link', _workspace, note)); | ||
const expected: string[] = [ | ||
'[first-document](first-document.md)', | ||
'[second-document](second-document.md)', | ||
'[[non-exist-file]]', | ||
'[#one section](<file-with-different-link-formats.md#one section>)', | ||
'[another name](<file-with-different-link-formats.md#one section>)', | ||
'[an alias](first-document.md)', | ||
'[first-document](first-document.md)', | ||
]; | ||
expect(actual.length).toEqual(expected.length); | ||
const _ = actual.map((LinkReplace, index) => { | ||
expect(LinkReplace.newText).toEqual(expected[index]); | ||
}); | ||
}); | ||
|
||
it('can generate wikilinks correctly', async () => { | ||
const note = findBySlug('file-with-different-link-formats'); | ||
const actual = note.links | ||
.filter(link => link.type === 'link') | ||
.map(link => convertLinkFormat(link, 'wikilink', _workspace, note)); | ||
const expected: string[] = ['[[first-document|file]]']; | ||
expect(actual.length).toEqual(expected.length); | ||
const _ = actual.map((LinkReplace, index) => { | ||
expect(LinkReplace.newText).toEqual(expected[index]); | ||
}); | ||
}); | ||
}); |
83 changes: 83 additions & 0 deletions
83
packages/foam-vscode/src/core/janitor/convert-links-format.ts
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,83 @@ | ||
import { Resource, ResourceLink } from '../model/note'; | ||
import { URI } from '../model/uri'; | ||
import { Range } from '../model/range'; | ||
import { FoamWorkspace } from '../model/workspace'; | ||
import { isNone } from '../utils'; | ||
import { MarkdownLink } from '../services/markdown-link'; | ||
|
||
export interface LinkReplace { | ||
newText: string; | ||
range: Range /* old range */; | ||
} | ||
|
||
/** | ||
* convert a link based on its workspace and the note containing it. | ||
* According to targetFormat parameter to decide output format. If link.type === targetFormat, then it simply copy | ||
* the rawText into LinkReplace. Therefore, it's recommended to filter before conversion. | ||
* If targetFormat isn't supported, or the target resource pointed by link cannot be found, the function will throw | ||
* exception. | ||
* @param link | ||
* @param targetFormat 'wikilink' | 'link' | ||
* @param workspace | ||
* @param note | ||
* @returns LinkReplace { newText: string; range: Range; } | ||
*/ | ||
export function convertLinkFormat( | ||
link: ResourceLink, | ||
targetFormat: 'wikilink' | 'link', | ||
workspace: FoamWorkspace, | ||
note: Resource | URI | ||
): LinkReplace { | ||
const resource = note instanceof URI ? workspace.find(note) : note; | ||
const targetUri = workspace.resolveLink(resource, link); | ||
/* If it's already the target format or a placeholder, no transformation happens */ | ||
if (link.type === targetFormat || targetUri.scheme === 'placeholder') { | ||
return { | ||
newText: link.rawText, | ||
range: link.range, | ||
}; | ||
} | ||
|
||
let { target, section, alias } = MarkdownLink.analyzeLink(link); | ||
let sectionDivider = section ? '#' : ''; | ||
|
||
if (isNone(targetUri)) { | ||
throw new Error( | ||
`Unexpected state: link to: "${link.rawText}" is not resolvable` | ||
); | ||
} | ||
|
||
const targetRes = workspace.find(targetUri); | ||
let relativeUri = targetRes.uri.relativeTo(resource.uri.getDirectory()); | ||
|
||
if (targetFormat === 'wikilink') { | ||
return MarkdownLink.createUpdateLinkEdit(link, { | ||
target: workspace.getIdentifier(relativeUri), | ||
type: 'wikilink', | ||
}); | ||
} | ||
|
||
if (targetFormat === 'link') { | ||
/* if alias is empty, construct one as target#section */ | ||
if (alias === '') { | ||
/* in page anchor have no filename */ | ||
if (relativeUri.getBasename() === resource.uri.getBasename()) { | ||
target = ''; | ||
} | ||
alias = `${target}${sectionDivider}${section}`; | ||
} | ||
|
||
/* if it's originally an embedded note, the markdown link shouldn't be embedded */ | ||
const isEmbed = targetRes.type === 'image' ? link.isEmbed : false; | ||
|
||
return MarkdownLink.createUpdateLinkEdit(link, { | ||
alias: alias, | ||
target: relativeUri.path, | ||
isEmbed: isEmbed, | ||
type: 'link', | ||
}); | ||
} | ||
throw new Error( | ||
`Unexpected state: targetFormat: ${targetFormat} is not supported` | ||
); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { generateLinkReferences } from './generate-link-references'; | ||
export { generateHeading } from './generate-headings'; | ||
export { convertLinkFormat } from './convert-links-format'; |
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
Oops, something went wrong.