diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7371d97..2bc48a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,14 +10,12 @@ on: branches: [ "main" ] jobs: - build: - - runs-on: ubuntu-latest - + tests: strategy: matrix: + os: [ubuntu-latest, windows-latest, macos-latest] node-version: [20.x] - + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} @@ -26,8 +24,6 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' - run: npm ci - - run: npm run lint - run: npm run build - - run: npm run check - run: npx playwright install - - run: npm test + - run: npm test \ No newline at end of file diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..09b661b --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +.svelte-kit \ No newline at end of file diff --git a/packages/process/.prettierignore b/packages/process/.prettierignore new file mode 100644 index 0000000..7730e2f --- /dev/null +++ b/packages/process/.prettierignore @@ -0,0 +1,2 @@ +dist +.turbo \ No newline at end of file diff --git a/packages/process/src/processor.ts b/packages/process/src/processor.ts index b5b8dfe..7802e7f 100644 --- a/packages/process/src/processor.ts +++ b/packages/process/src/processor.ts @@ -27,13 +27,17 @@ const processor = ({ /** * Only use on specific extensions */ - if (!extensions.find((extension) => filename?.endsWith(extension))) + if ( + !filename || + !extensions.find((extension) => filename?.endsWith(extension)) + ) return; /** * Add svelte components to be used with markdoc tags */ const code = transformer({ + filename, config, content, layouts, diff --git a/packages/process/src/transformer.ts b/packages/process/src/transformer.ts index 235cb68..6df9207 100644 --- a/packages/process/src/transformer.ts +++ b/packages/process/src/transformer.ts @@ -16,11 +16,17 @@ import { getNameOfDeclaration, isVariableStatement, } from 'typescript'; -import { dirname, join } from 'path'; +import { dirname, join, relative } from 'path'; import { load as loadYaml } from 'js-yaml'; import { parse as svelteParse, walk } from 'svelte/compiler'; import { render_html } from './renderer'; -import { get_all_files, path_exists, read_file, write_to_file } from './utils'; +import { + get_all_files, + path_exists, + read_file, + relative_posix_path, + write_to_file, +} from './utils'; import * as default_schema from './default_schema'; import type { Config } from './config'; @@ -31,6 +37,7 @@ type Var = { export function transformer({ content, + filename, nodes_file, tags_file, partials_dir, @@ -39,6 +46,7 @@ export function transformer({ config, }: { content: string; + filename: string; nodes_file: Config['nodes']; tags_file: Config['tags']; partials_dir: Config['partials']; @@ -66,7 +74,6 @@ export function transformer({ ? layouts[frontmatter?.layout ?? 'default'] ?? undefined : undefined; const has_layout = selected_layout !== undefined; - /** * add used svelte components to the script tag */ @@ -80,22 +87,31 @@ export function transformer({ /** * add import for tags */ - if (has_tags) { - dependencies += `import * as INTERNAL__TAGS from '${tags_file}';`; + if (tags_file && has_tags) { + dependencies += `import * as INTERNAL__TAGS from '${relative_posix_path( + filename, + tags_file, + )}';`; } /** * add import for nodes */ - if (has_nodes) { - dependencies += `import * as INTERNAL__NODES from '${nodes_file}';`; + if (nodes_file && has_nodes) { + dependencies += `import * as INTERNAL__NODES from '${relative_posix_path( + filename, + nodes_file, + )}';`; } /** * add import for layout */ - if (has_layout) { - dependencies += `import INTERNAL__LAYOUT from '${selected_layout}';`; + if (selected_layout && has_layout) { + dependencies += `import INTERNAL__LAYOUT from '${relative_posix_path( + filename, + selected_layout, + )}';`; } if (generate_schema) { diff --git a/packages/process/src/utils.ts b/packages/process/src/utils.ts index 750d341..c03b5a3 100644 --- a/packages/process/src/utils.ts +++ b/packages/process/src/utils.ts @@ -5,7 +5,8 @@ import { readdirSync, writeFileSync, } from 'fs'; -import { join } from 'path'; +import { dirname, join, relative, sep } from 'path'; +import { sep as posix_sep } from 'path/posix'; export function get_all_files(path: string): string[] { const files = []; @@ -29,3 +30,7 @@ export function write_to_file(file: string, content: string): void { export function path_exists(path: string): boolean { return existsSync(path); } + +export function relative_posix_path(from: string, to: string): string { + return relative(dirname(from), to).split(sep).join(posix_sep); +} diff --git a/packages/process/tests/processor.test.mjs b/packages/process/tests/processor.test.mjs index 3b2a22c..f058008 100644 --- a/packages/process/tests/processor.test.mjs +++ b/packages/process/tests/processor.test.mjs @@ -3,7 +3,7 @@ import assert from 'node:assert/strict'; import { test } from 'node:test'; import { basename, dirname, join } from 'node:path'; import { markdoc } from '../dist/module.js'; -import { absoulute, read_file } from './utils.mjs'; +import { absoulute, read_file, relative_posix_path } from './utils.mjs'; import { fileURLToPath } from 'node:url'; test('preprocessor', async (context) => { @@ -60,25 +60,26 @@ test('preprocessor', async (context) => { ); }); - const stream = fg.globStream(absoulute(import.meta.url, './processor/**'), { + const files = fg.globSync('tests/processor/**', { onlyDirectories: true, }); - const current_dir = dirname(fileURLToPath(import.meta.url)); - for await (const entry of stream) { - await context.test('tests ' + basename(entry), async () => { - const before = await read_file(join(entry, 'source.markdoc')); - const after = await read_file(join(entry, 'compiled.txt')); - const preprocess = await import(join(entry, 'config.mjs')).then( - (m) => m.default, - ); - const markup = await preprocess.markup({ - content: before, - filename: 'test.markdoc', + + assert(files.length > 0, 'no test files found'); + + await Promise.all( + files.map(async (entry) => { + return context.test('tests ' + basename(entry), async () => { + const before = await read_file(join(entry, 'source.markdoc')); + const after = await read_file(join(entry, 'compiled.txt')); + const preprocess = await import( + '../' + join(entry, 'config.mjs') + ).then((m) => m.default); + const markup = await preprocess.markup({ + content: before, + filename: 'test.markdoc', + }); + assert.equal(markup.code, after); }); - assert.equal( - markup.code, - after.replaceAll('__PATH__', current_dir), - ); - }); - } + }), + ); }); diff --git a/packages/process/tests/processor/layout - default/compiled.txt b/packages/process/tests/processor/layout - default/compiled.txt index 6734945..0fdb2d3 100644 --- a/packages/process/tests/processor/layout - default/compiled.txt +++ b/packages/process/tests/processor/layout - default/compiled.txt @@ -1 +1 @@ -

Hello World

\ No newline at end of file +

Hello World

\ No newline at end of file diff --git a/packages/process/tests/processor/layout - named/compiled.txt b/packages/process/tests/processor/layout - named/compiled.txt index 257b6c6..fc99345 100644 --- a/packages/process/tests/processor/layout - named/compiled.txt +++ b/packages/process/tests/processor/layout - named/compiled.txt @@ -1 +1 @@ -

Hello World

\ No newline at end of file +

Hello World

\ No newline at end of file diff --git a/packages/process/tests/processor/nodes, tags and partials/compiled.txt b/packages/process/tests/processor/nodes, tags and partials/compiled.txt index 2ba2422..94ac4ab 100644 --- a/packages/process/tests/processor/nodes, tags and partials/compiled.txt +++ b/packages/process/tests/processor/nodes, tags and partials/compiled.txt @@ -1 +1 @@ -
Heading 1Heading 2With ID With Class

slot content

I am a partialLorem IpsumI am nested
\ No newline at end of file +
Heading 1Heading 2With ID With Class

slot content

I am a partialLorem IpsumI am nested
\ No newline at end of file diff --git a/packages/process/tests/processor/nodes/compiled.txt b/packages/process/tests/processor/nodes/compiled.txt index 17b07af..5ace9da 100644 --- a/packages/process/tests/processor/nodes/compiled.txt +++ b/packages/process/tests/processor/nodes/compiled.txt @@ -1 +1 @@ -
Heading 1Heading 2With ID With Class
\ No newline at end of file +
Heading 1Heading 2With ID With Class
\ No newline at end of file diff --git a/packages/process/tests/processor/tags/compiled.txt b/packages/process/tests/processor/tags/compiled.txt index fab4c12..38ac354 100644 --- a/packages/process/tests/processor/tags/compiled.txt +++ b/packages/process/tests/processor/tags/compiled.txt @@ -1 +1 @@ -

slot content

\ No newline at end of file +

slot content

\ No newline at end of file diff --git a/packages/process/tests/utils.mjs b/packages/process/tests/utils.mjs index e713429..6200ce3 100644 --- a/packages/process/tests/utils.mjs +++ b/packages/process/tests/utils.mjs @@ -1,13 +1,7 @@ -import { readFile } from 'fs/promises'; import { dirname, join } from 'path'; import { fileURLToPath } from 'url'; +export { relative_posix_path, read_file } from '../dist/utils.js'; export function absoulute(current, file) { return join(dirname(fileURLToPath(current)), file); } - -export function read_file(file) { - return readFile(file, { - encoding: 'utf8', - }); -}