From 60b4adf3534602c53c77fe0f91e3d87d66e5f473 Mon Sep 17 00:00:00 2001 From: XinglanL Date: Fri, 21 Jun 2024 18:12:27 +0800 Subject: [PATCH 01/23] inseart new test file --- .../janitor/generate-link-references.test.ts | 2 +- .../file-with-different-link-formats.md | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 packages/foam-vscode/test-data/__scaffold__/file-with-different-link-formats.md diff --git a/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts b/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts index 708f4be4c..eaebf7cc5 100644 --- a/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts +++ b/packages/foam-vscode/src/core/janitor/generate-link-references.test.ts @@ -36,7 +36,7 @@ describe('generateLinkReferences', () => { }); it('initialised test graph correctly', () => { - expect(_workspace.list().length).toEqual(10); + expect(_workspace.list().length).toEqual(11); }); it('should add link references to a file that does not have them', async () => { diff --git a/packages/foam-vscode/test-data/__scaffold__/file-with-different-link-formats.md b/packages/foam-vscode/test-data/__scaffold__/file-with-different-link-formats.md new file mode 100644 index 000000000..3ba8e9e74 --- /dev/null +++ b/packages/foam-vscode/test-data/__scaffold__/file-with-different-link-formats.md @@ -0,0 +1,21 @@ +# File with different link formats + +markdown link [home page](https://foambubble.github.io/) + +wikilink to file [[first-document]]. + +markdown format link to local [file](first-document.md) + +embeded wikilink to file ![[second-document]]. + +wikilink to placeholder [[non-exist-file]] + +in-note anchor [[file-with-different-link-formats#one section]] + +alias to anchor [[file-with-different-link-formats#one section|another name]] + +alias [[first-document|an alias]] + +dupilcated wikilink to file [[first-document]] + +# one section \ No newline at end of file From bab6cd7327e350de91f9943e52b8acab5171569c Mon Sep 17 00:00:00 2001 From: XinglanL Date: Fri, 21 Jun 2024 18:17:23 +0800 Subject: [PATCH 02/23] add generate standalone note command --- .../src/core/janitor/generate-mdlinks.test.ts | 55 +++++++++ .../src/core/janitor/generate-mdlinks.ts | 107 ++++++++++++++++++ .../foam-vscode/src/core/janitor/index.ts | 1 + .../commands/generate-standalone-note.ts | 73 ++++++++++++ .../src/features/commands/index.ts | 1 + 5 files changed, 237 insertions(+) create mode 100644 packages/foam-vscode/src/core/janitor/generate-mdlinks.test.ts create mode 100644 packages/foam-vscode/src/core/janitor/generate-mdlinks.ts create mode 100644 packages/foam-vscode/src/features/commands/generate-standalone-note.ts diff --git a/packages/foam-vscode/src/core/janitor/generate-mdlinks.test.ts b/packages/foam-vscode/src/core/janitor/generate-mdlinks.test.ts new file mode 100644 index 000000000..ad8a5af1a --- /dev/null +++ b/packages/foam-vscode/src/core/janitor/generate-mdlinks.test.ts @@ -0,0 +1,55 @@ +import { generateMarkdownLinks } from './generate-mdlinks'; +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 = await generateMarkdownLinks(note, _workspace); + const expected: string[] = [ + '[first-document](first-document.md "First Document")', + '[second-document](second-document.md "Second Document")', + '[#one section](<#one section> "File with different link formats")', + '[another name](<#one section> "File with different link formats")', + '[an alias](first-document.md "First Document")', + ]; + expect(actual.length).toEqual(expected.length); + const _ = actual.map((textReplace, index) => { + expect(textReplace.to).toEqual(expected[index]); + }); + }); +}); diff --git a/packages/foam-vscode/src/core/janitor/generate-mdlinks.ts b/packages/foam-vscode/src/core/janitor/generate-mdlinks.ts new file mode 100644 index 000000000..3cc6ed395 --- /dev/null +++ b/packages/foam-vscode/src/core/janitor/generate-mdlinks.ts @@ -0,0 +1,107 @@ +import { NoteLinkDefinition, Resource } from '../model/note'; +import { FoamWorkspace } from '../model/workspace'; +import { createMarkdownReferences } from '../services/markdown-provider'; + +export interface TextReplace { + from: string; + to: string; +} + +/** + * parse wikilink's rawText + * possible inpout: + * 1. [[\#\|\]] + * 2. [[#\|\]] + * 3. [[\#\]] + * 4. [[\|\]] + */ +class WikilinkRawText { + private lable_m: string = ''; + private filename_m: string = ''; + private section_m: string = ''; + + public readFromText(definition: NoteLinkDefinition): WikilinkRawText { + const struct_rawText_regex = + /(?:(?[^\]|#]+)?(?:#(?
[^\]|]+))?)?(?:\|(?