Compare commits
59 Commits
951949f98d
...
3cff1ec6f0
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cff1ec6f0 | |||
| 6ed883a8b4 | |||
| 38af6d3f0b | |||
| 6e50034cab | |||
| c13b947628 | |||
| e0fa31b7a1 | |||
| ed614026ec | |||
| 87f8786c43 | |||
| acae07b8f3 | |||
| 1b100483de | |||
| e03fd78ea9 | |||
| 370ef205a9 | |||
| d328ddb749 | |||
| 5dc5590da9 | |||
| 364112a0a4 | |||
| a38523e933 | |||
| 200672dae2 | |||
| 3d3aad88c8 | |||
| e4e43d1a83 | |||
| 737a9ec314 | |||
| 1ebe0380ee | |||
| d62d71b5d1 | |||
| b36e0ea503 | |||
| bc4d8fa37e | |||
| d2b21e7405 | |||
| e3c192359f | |||
| 46532e4c85 | |||
| c722135a56 | |||
| 887fa863bc | |||
| 74d71cfb6a | |||
| ca7e7cddd3 | |||
| 94431a2aa9 | |||
| 5b1f20c5bc | |||
| d0c566f8df | |||
| 5e586aa49a | |||
| 66b2743d3d | |||
| 3723b2b5e6 | |||
| ce788bfd50 | |||
| 230a6278cc | |||
| 42d6e0c198 | |||
| e1af2979af | |||
| f6dedc6f10 | |||
| cdf0a49550 | |||
| c3c0650210 | |||
| d077f700d8 | |||
| ec752b1c66 | |||
| 46aef4f12f | |||
| 1b29e328cf | |||
| e50cb819f3 | |||
| c32e38ca10 | |||
| d7c8c7a43a | |||
| 1cd3edc90c | |||
| 0e768c9f61 | |||
| 1fcd775cc5 | |||
| 203a1ed147 | |||
| ef1914ee5c | |||
| 084b101e31 | |||
| b2db128aa9 | |||
| 081e880fb6 |
36
cmd/backend/create_toc.lua
Normal file
36
cmd/backend/create_toc.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
-- Helper function: remove all image inlines from a list of inlines.
|
||||
local function remove_images(inlines)
|
||||
local result = {}
|
||||
for _, item in ipairs(inlines) do
|
||||
if item.t ~= "Image" then
|
||||
table.insert(result, item)
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
-- Build a bullet list representing the table of contents.
|
||||
local function build_toc(doc)
|
||||
local toc_items = {}
|
||||
for _, block in ipairs(doc.blocks) do
|
||||
if block.t == "Header" then
|
||||
local clean_inlines = remove_images(block.content)
|
||||
local header_text = pandoc.utils.stringify(clean_inlines)
|
||||
if header_text ~= "" then
|
||||
local link = pandoc.Link(clean_inlines, "#" .. block.identifier)
|
||||
table.insert(toc_items, { link })
|
||||
end
|
||||
end
|
||||
end
|
||||
return pandoc.BulletList(toc_items)
|
||||
end
|
||||
|
||||
-- The Pandoc function runs after the document is fully constructed.
|
||||
function Pandoc(doc)
|
||||
local toc = build_toc(doc)
|
||||
-- Insert the TOC at the very beginning of the document.
|
||||
table.insert(doc.blocks, 1, toc)
|
||||
return doc
|
||||
end
|
||||
|
||||
@@ -20,8 +20,9 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
// --toc
|
||||
articleFileName := filepath.Join(os.TempDir(), fmt.Sprint(uuid.New(), ".md"))
|
||||
cmd := exec.Command("pandoc", "-s", "-f", "docx", "-t", "commonmark_x", "-o", articleFileName, "--extract-media", tmpDir, filename) // TODO: Is writing to a file necessary?
|
||||
cmd := exec.Command("pandoc", "-s", "--lua-filter=cmd/backend/create_toc.lua", "-f", "docx", "-t", "commonmark_x", "-o", articleFileName, "--extract-media", tmpDir, filename) // TODO: Is writing to a file necessary?
|
||||
cmd.Stderr = &stderr
|
||||
if err = cmd.Run(); err != nil {
|
||||
return nil, fmt.Errorf("error converting docx to markdown: %v: %v", err, stderr.String())
|
||||
@@ -33,6 +34,9 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("error reading markdown file: %v", err)
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`\{width=[^}]+height=[^}]+\}`)
|
||||
articleContent = re.ReplaceAll(articleContent, []byte(""))
|
||||
|
||||
imageNames, err := filepath.Glob(filepath.Join(tmpDir, "media", "*"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting docx images from temporary directory: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user