/notion_import.go

https://github.com/essentialbooks/books · Go · 62 lines · 52 code · 6 blank · 4 comment · 20 complexity · c1ef24abcaa2a6e342f75ceb582286a7 MD5 · raw file

  1. package main
  2. import (
  3. "github.com/kjk/notionapi"
  4. )
  5. // convert 2131b10c-ebf6-4938-a127-7089ff02dbe4 to 2131b10cebf64938a1277089ff02dbe4
  6. // TODO: replace with direct use of notionapi.ToNoDashID
  7. func toNoDashID(id string) string {
  8. return notionapi.ToNoDashID(id)
  9. }
  10. func updateFormatIfNeeded(page *notionapi.Page) bool {
  11. // can't write back without a token
  12. if notionAuthToken == "" {
  13. return false
  14. }
  15. args := map[string]interface{}{}
  16. format := page.Root().FormatPage()
  17. if format == nil || !format.PageSmallText {
  18. args["page_small_text"] = true
  19. }
  20. if format == nil || !format.PageFullWidth {
  21. args["page_full_width"] = true
  22. }
  23. if len(args) == 0 {
  24. return false
  25. }
  26. logf(" updating format to %v\n", args)
  27. err := page.SetFormat(args)
  28. if err != nil {
  29. logf("updateFormatIfNeeded: page.SetFormat() failed with '%s'\n", err)
  30. }
  31. return true
  32. }
  33. func updateTitleIfNeeded(page *notionapi.Page) bool {
  34. // can't write back without a token
  35. if notionAuthToken == "" {
  36. return false
  37. }
  38. newTitle := cleanTitle(page.Root().Title)
  39. if newTitle == page.Root().Title {
  40. return false
  41. }
  42. logf(" updating title to '%s'\n", newTitle)
  43. err := page.SetTitle(newTitle)
  44. if err != nil {
  45. logf("updateTitleIfNeeded: page.SetTitle() failed with '%s'\n", err)
  46. }
  47. return true
  48. }
  49. func updateFormatOrTitleIfNeeded(page *notionapi.Page) bool {
  50. updated1 := updateFormatIfNeeded(page)
  51. updated2 := updateTitleIfNeeded(page)
  52. return updated1 || updated2
  53. }
  54. func isIDEqual(id1, id2 string) bool {
  55. return notionapi.ToNoDashID(id1) == notionapi.ToNoDashID(id2)
  56. }