Compare commits

..

8 Commits

9 changed files with 80 additions and 3 deletions

View File

@ -175,6 +175,10 @@ func (db *DB) GetCertainArticles(attribute string, value bool) ([]*Article, erro
articleList = append(articleList, article)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
return articleList, nil
}
@ -243,6 +247,13 @@ func (db *DB) GetCurrentIssueArticles() ([]*Article, error) {
articleList = append(articleList, article)
}
if err = rows.Err(); err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr)
}
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
if err = tx.Commit(); err != nil {
return nil, fmt.Errorf("error committing transaction when getting articles of issue %v: %v", issueID, err)
}

View File

@ -68,6 +68,10 @@ func (db *DB) GetArticleAuthors(c *Config, articleID int64) ([]*User, error) {
authors = append(authors, author)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
return authors, nil
}

View File

@ -68,6 +68,10 @@ func (db *DB) GetArticleContributors(c *Config, articleID int64) ([]*User, error
contributors = append(contributors, contributor)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
return contributors, nil
}

View File

@ -61,6 +61,10 @@ func (db *DB) GetArticleTags(articleID int64) ([]*Tag, error) {
tags = append(tags, tag)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
return tags, nil
}

View 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

View File

@ -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)

View File

@ -31,5 +31,9 @@ func (db *DB) GetTagList() ([]*Tag, error) {
tagList = append(tagList, tag)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
return tagList, nil
}

View File

@ -117,6 +117,9 @@ func aesDecrypt(c *Config, ciphertext string) (string, error) {
}
nonceSize := gcm.NonceSize()
if len(data) < nonceSize {
return "", errors.New("ciphertext too short")
}
nonce, cipherText := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, cipherText, nil)
@ -450,8 +453,6 @@ func (db *DB) AddFirstUser(c *Config, u *User, pass string) (int64, error) {
func (db *DB) GetAllUsers(c *Config) ([]*User, error) {
var aesFirstName, aesLastName, aesEmail string
var err error
query := "SELECT id, username, first_name, last_name, email, profile_pic_link, role FROM users"
rows, err := db.Query(query)
@ -484,6 +485,10 @@ func (db *DB) GetAllUsers(c *Config) ([]*User, error) {
users = append(users, user)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
return users, nil
}
@ -523,6 +528,10 @@ func (db *DB) GetAllUsersMap(c *Config) (map[int64]*User, error) {
users[user.ID] = user
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over rows: %v", err)
}
return users, nil
}

View File

@ -901,6 +901,7 @@ func AllowEditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Handler
}
newArticle := *oldArticle
newArticle.UUID = uuid.New()
newArticle.Published = false
newArticle.Rejected = true
newArticle.EditedID = oldArticle.ID