Skip to content

Instantly share code, notes, and snippets.

@tictaqqn
Last active April 2, 2024 13:53
Show Gist options
  • Save tictaqqn/76d79766e053c2df3b16192460482733 to your computer and use it in GitHub Desktop.
Save tictaqqn/76d79766e053c2df3b16192460482733 to your computer and use it in GitHub Desktop.
NotionToSlack
import AppKit
/*
* A command to address issues when transferring bulleted lists from Notion to Slack,
* where nested bullet points do not maintain their structure correctly.
* Examples of bullet hierarchy:
* - one
* - two
* - three
*
* @author @Bison0802-2, @tictaqqn
* @license MIT
*/
func alignHtml(_ html: String) -> String {
var newHtml = html
let replacements = [
("<h1>", "<p><b>"), ("</h1>", "</b></p>"),
("<h2>", "<p><b>"), ("</h2>", "</b></p>"),
("<h3>", "<p><b>"), ("</h3>", "</b></p>"),
("</li>", ""),
("\n\n", "\n")
]
for (original, replacement) in replacements {
newHtml = newHtml.replacingOccurrences(of: original, with: replacement)
}
// Regex replacement for <li> tags
let regex = try! NSRegularExpression(pattern: "<li>(.+?)\n", options: [])
newHtml = regex.stringByReplacingMatches(in: newHtml, options: [], range: NSRange(newHtml.startIndex..., in: newHtml), withTemplate: "<li>$1</li>\n")
return newHtml
}
func notionToSlack() {
let pb = NSPasteboard.general
guard let html = pb.string(forType: .html) else {
return
}
guard let plain = pb.string(forType: .string) else {
return
}
let alignedHtml = alignHtml(html)
// Clearing the clipboard
pb.clearContents()
pb.declareTypes([.string, .html], owner: nil)
// Setting new strings to the clipboard
pb.setData(alignedHtml.data(using: .utf8), forType: .html)
pb.setData(plain.data(using: .utf8), forType: .string)
}
notionToSlack()
@tictaqqn
Copy link
Author

tictaqqn commented Apr 2, 2024

co-authored by @Bison0802-2

@tictaqqn
Copy link
Author

tictaqqn commented Apr 2, 2024

Run below to compile this code:

swiftc NotionToSlack.swift -O -o NotionToSlack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment