Skip to content

Commit ad9c051

Browse files
committed
More robust on posts in subfolders
1 parent ade3178 commit ad9c051

File tree

4 files changed

+80
-40
lines changed

4 files changed

+80
-40
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ This will install everything you need for your Svelte components to work to the
2929
Here's the short version of adding Svelte components to your Quarto docs:
3030

3131
1. Add a list of Svelte components (eg. `Circles.svelte`) you want to add to your document frontmatter under `svelte`
32-
2. Add a magic placeholder div to your document using the `.svelteimport` class and a [Quarto include](https://quarto.org/docs/authoring/includes.html) to the path to your Quarto doc, prefixed with `.sverto/`. For example:
32+
2. Add a magic placeholder div to your document using the `.svelteimport` class and a [Quarto include](https://quarto.org/docs/authoring/includes.html) to the path to your Quarto doc, prefixed with `/.sverto/`. For example:
3333

3434
````
3535
:::{.svelteimport}
36-
{{< include .sverto/example.qmd >}}
36+
{{< include /.sverto/example.qmd >}}
3737
:::
3838
````
3939

_extensions/sverto/create-imports.lua

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,89 @@
11
-- ref: https://pandoc.org/lua-filters.html#pandoc-module
22

3-
-- create .sverto
4-
os.execute("mkdir .sverto/")
3+
-- use mkdir (windows) or mkdir -p (*nix) to create directories
4+
-- from https://stackoverflow.com/a/14425862/3246758
5+
function get_path_sep()
6+
return package.config:sub(1, 1)
7+
end
8+
9+
function create_dir_recursively(path)
10+
print("Creating " .. path)
11+
local path_separator = get_path_sep()
12+
if path_separator == "\\" or path_separator == "\"" then
13+
-- windows
14+
-- NOTE - there is one edge case where folders might not be made
15+
-- recursively, https://www.robvanderwoude.com/cmdextmsg.php
16+
os.execute("mkdir " .. path)
17+
else
18+
-- macos/linux
19+
os.execute("mkdir -p " .. path)
20+
end
21+
end
522

6-
-- get the input files
23+
-- path_dir: extract the folder path from a file path
24+
-- from https://stackoverflow.com/a/9102300/3246758
25+
function path_dir(path)
26+
return path:match("(.*".. get_path_sep() ..")") or ""
27+
end
728

29+
-- create .sverto
30+
create_dir_recursively(".sverto/")
31+
32+
-- get the input files as a table
833
in_file_string = os.getenv("QUARTO_PROJECT_INPUT_FILES")
934
in_files = {}
1035
for in_file in string.gmatch(in_file_string, "([^\n]+)") do
1136
table.insert(in_files, in_file)
1237
end
1338

14-
-- test case: single file (we want to do this for all files!)
15-
16-
first_file_name = in_files[1]
17-
first_file = io.open(first_file_name, "r")
18-
first_file_contents = pandoc.read(io.output(first_file):read("a"))
19-
20-
-- check if there's svelte in the frontmatter
21-
22-
svelte_key = first_file_contents.meta["svelte"]
39+
-- create_imports: given a quarto doc path, writes the svelte import
40+
-- declarations to .svelte/[path]
41+
function create_imports(quarto_doc_name)
42+
local doc_handle = io.open(quarto_doc_name, "r")
43+
local doc_content = pandoc.read(io.output(doc_handle):read("a"))
44+
45+
-- check if there's svelte in the frontmatter
46+
47+
local svelte_key = doc_content.meta["svelte"]
48+
49+
if (svelte_key) then
50+
local import_block = "```{ojs}\n"
2351

24-
if (svelte_key) then
25-
import_block = "```{ojs}\n"
26-
for i, item in ipairs(svelte_key) do
27-
-- TODO - assert item is a pandoc string
52+
-- write each svelte file in this doc's frontmatter out to
53+
-- (a) the require block of this doc, and
54+
-- (b) the global imports file going to the svelte compiler
55+
for i, item in ipairs(svelte_key) do
56+
-- TODO - assert item is a pandoc string
57+
58+
local item_string = pandoc.utils.stringify(item)
59+
local item_js = string.sub(item_string, 1, string.len(item_string) - 7)
60+
61+
-- write the item out to .sverto/.svelte-imports
62+
local import_list_file = io.open(".sverto/.sverto-imports", "a")
63+
io.output(import_list_file):write(item_string .. "\n")
64+
io.close(import_list_file)
65+
66+
-- add the item to the require block
67+
import_block = import_block ..
68+
item_js .. " = import(\"/" .. item_js ..".js\")\n"
69+
70+
end
2871

29-
item_string = pandoc.utils.stringify(item)
30-
item_js = string.sub(item_string, 1, string.len(item_string) - 7)
31-
32-
-- write the item out to .sverto/.svelte-imports
33-
import_list_file = io.open(".sverto/.sverto-imports", "a")
34-
io.output(import_list_file):write(item_string .. "\n")
35-
io.close(import_list_file)
36-
37-
-- add the item to the require block
38-
import_block = import_block ..
39-
item_js .. " = import(\"/" .. item_js ..".js\")\n"
40-
72+
-- write the import block (inc. the final backticks) out to .sverto/[path]
73+
create_dir_recursively(".sverto/" .. path_dir(quarto_doc_name))
74+
local import_block = import_block .. "```"
75+
local import_file = io.open(".sverto/" .. quarto_doc_name, "w")
76+
io.output(import_file):write(import_block .. "\n")
77+
io.close(import_file)
4178
end
42-
43-
-- write the import block out to .sverto/[path]
44-
import_block = import_block .. "```"
45-
import_file = io.open(".sverto/" .. first_file_name, "w")
46-
io.output(import_file):write(import_block .. "\n")
47-
io.close(import_file)
4879
end
4980

50-
-- write the output dir temporarily
81+
-- create the imports for each quarto doc
82+
for key, value in ipairs(in_files) do
83+
create_imports(value)
84+
end
85+
86+
-- write the output dir path temporarily (so rollup can use it)
5187
outdir_file = io.open(".sverto/.sverto-outdir", "w")
5288
io.output(outdir_file):write(
5389
os.getenv("QUARTO_PROJECT_OUTPUT_DIR"))

_extensions/sverto/refresh.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
// delete the temporary import block files and the temp render metadata
2-
Deno.remove(".sverto/", { recursive: true })
2+
try {
3+
await Deno.remove(".sverto/", { recursive: true })
4+
} catch {
5+
6+
}

example.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ Add a magic block anywhere in your document that looks like this:
2626

2727
````markdown
2828
:::{.svelteimport}
29-
{{< import .sverto/example.qmd >}}
29+
{{< import /.sverto/example.qmd >}}
3030
:::
3131
````
3232

3333
When this document renders, it'll turn into an OJS block with a series of statements like `X = import("X.js")`:
3434

3535
:::{.svelteimport}
36-
{{< include .sverto/example.qmd >}}
36+
{{< include /.sverto/example.qmd >}}
3737
:::
3838

3939
Replace `example.qmd` with the path to your Quarto doc within the project. For example:

0 commit comments

Comments
 (0)