Skip to content

Commit

Permalink
fix: posix paths (#10)
Browse files Browse the repository at this point in the history
* fix: posix paths

* fix: git attribute

* revert: gitattributes

* try pretteier setting

* fix: linter

* test

* fix: tests

* chore: run formattter

* fix: test order

* add prettierignore

* fix linter

* fix: posix paths

* ci: add macos to tests

* split tests

* ci: checkout before

* revert: changes
  • Loading branch information
TorstenDittmann authored Sep 4, 2023
1 parent 75baf10 commit f699b20
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
12 changes: 4 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.svelte-kit
2 changes: 2 additions & 0 deletions packages/process/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
.turbo
6 changes: 5 additions & 1 deletion packages/process/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 25 additions & 9 deletions packages/process/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -31,6 +37,7 @@ type Var = {

export function transformer({
content,
filename,
nodes_file,
tags_file,
partials_dir,
Expand All @@ -39,6 +46,7 @@ export function transformer({
config,
}: {
content: string;
filename: string;
nodes_file: Config['nodes'];
tags_file: Config['tags'];
partials_dir: Config['partials'];
Expand Down Expand Up @@ -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
*/
Expand All @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion packages/process/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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);
}
39 changes: 20 additions & 19 deletions packages/process/tests/processor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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),
);
});
}
}),
);
});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script>import INTERNAL__LAYOUT from '__PATH__/layouts/default.svelte';</script><INTERNAL__LAYOUT><article><h1>Hello World</h1></article></INTERNAL__LAYOUT>
<script>import INTERNAL__LAYOUT from 'tests/layouts/default.svelte';</script><INTERNAL__LAYOUT><article><h1>Hello World</h1></article></INTERNAL__LAYOUT>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script context="module">export const frontmatter = {"layout":"named"};</script><script>import INTERNAL__LAYOUT from '__PATH__/layouts/named.svelte';</script><INTERNAL__LAYOUT {...frontmatter}><article><h1>Hello World</h1></article></INTERNAL__LAYOUT>
<script context="module">export const frontmatter = {"layout":"named"};</script><script>import INTERNAL__LAYOUT from 'tests/layouts/named.svelte';</script><INTERNAL__LAYOUT {...frontmatter}><article><h1>Hello World</h1></article></INTERNAL__LAYOUT>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script>import * as INTERNAL__TAGS from '__PATH__/tags/module.svelte';import * as INTERNAL__NODES from '__PATH__/nodes/module.svelte';</script><article><INTERNAL__NODES.Heading level={1}>Heading 1</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={2}>Heading 2</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading id="my-id" level={1}>With ID </INTERNAL__NODES.Heading><INTERNAL__NODES.Heading class="my-class" level={1}>With Class</INTERNAL__NODES.Heading><INTERNAL__TAGS.Hello></INTERNAL__TAGS.Hello><p><INTERNAL__TAGS.Slot>slot content</INTERNAL__TAGS.Slot></p><INTERNAL__NODES.Heading level={1}>I am a partial</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={1}>Lorem Ipsum</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={1}>I am nested</INTERNAL__NODES.Heading></article>
<script>import * as INTERNAL__TAGS from 'tests/tags/module.svelte';import * as INTERNAL__NODES from 'tests/nodes/module.svelte';</script><article><INTERNAL__NODES.Heading level={1}>Heading 1</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={2}>Heading 2</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading id="my-id" level={1}>With ID </INTERNAL__NODES.Heading><INTERNAL__NODES.Heading class="my-class" level={1}>With Class</INTERNAL__NODES.Heading><INTERNAL__TAGS.Hello></INTERNAL__TAGS.Hello><p><INTERNAL__TAGS.Slot>slot content</INTERNAL__TAGS.Slot></p><INTERNAL__NODES.Heading level={1}>I am a partial</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={1}>Lorem Ipsum</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={1}>I am nested</INTERNAL__NODES.Heading></article>
2 changes: 1 addition & 1 deletion packages/process/tests/processor/nodes/compiled.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script>import * as INTERNAL__NODES from '__PATH__/nodes/module.svelte';</script><article><INTERNAL__NODES.Heading level={1}>Heading 1</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={2}>Heading 2</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading id="my-id" level={1}>With ID </INTERNAL__NODES.Heading><INTERNAL__NODES.Heading class="my-class" level={1}>With Class</INTERNAL__NODES.Heading></article>
<script>import * as INTERNAL__NODES from 'tests/nodes/module.svelte';</script><article><INTERNAL__NODES.Heading level={1}>Heading 1</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading level={2}>Heading 2</INTERNAL__NODES.Heading><INTERNAL__NODES.Heading id="my-id" level={1}>With ID </INTERNAL__NODES.Heading><INTERNAL__NODES.Heading class="my-class" level={1}>With Class</INTERNAL__NODES.Heading></article>
2 changes: 1 addition & 1 deletion packages/process/tests/processor/tags/compiled.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script>import * as INTERNAL__TAGS from '__PATH__/tags/module.svelte';</script><article><INTERNAL__TAGS.Hello></INTERNAL__TAGS.Hello><p><INTERNAL__TAGS.Slot>slot content</INTERNAL__TAGS.Slot></p></article>
<script>import * as INTERNAL__TAGS from 'tests/tags/module.svelte';</script><article><INTERNAL__TAGS.Hello></INTERNAL__TAGS.Hello><p><INTERNAL__TAGS.Slot>slot content</INTERNAL__TAGS.Slot></p></article>
8 changes: 1 addition & 7 deletions packages/process/tests/utils.mjs
Original file line number Diff line number Diff line change
@@ -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',
});
}

0 comments on commit f699b20

Please sign in to comment.