-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMarkdownLib.applescript
More file actions
97 lines (81 loc) · 2.5 KB
/
MarkdownLib.applescript
File metadata and controls
97 lines (81 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on isLink(linkText)
-- This is a very crude test if a string is a link.
ignoring case
return linkText begins with "http:" or ¬
linkText begins with "https:" or ¬
linkText begins with "ftp:" or ¬
linkText ends with ".com"
end ignoring
end isLink
on isEmail(emailText)
-- This is a very crude test if a string is an email address.
ignoring case
return emailText contains "@" and emailText ends with ".com"
end ignoring
end isEmail
on isBoldFont(fontName)
ignoring case
return fontName contains "Bold"
end ignoring
end isBoldFont
on isItalicFont(fontName)
ignoring case
return fontName contains "Italic"
end ignoring
end isItalicFont
on richTextToMarkdown(cellRef)
local markdownText, inBold, inItalic, rowText
set markdownText to ""
set inBold to false
set inItalic to false
set rowText to {} -- use an array because its a bit faster
using terms from application "TextEdit"
repeat with anAttribute in attribute runs of cellRef
set {runText, runIsBold, runIsItalic} to ¬
{contents of anAttribute, my isBoldFont(font of anAttribute), my isItalicFont(font of anAttribute)}
-- close any existing attribute runs that have ended
if inBold and not runIsBold then
set end of rowText to "**"
set inBold to false
end if
if inItalic and not runIsItalic then
set end of rowText to "*"
set inItalic to false
end if
-- open new attribute runs
if not inBold and runIsBold then
set end of rowText to "**"
set inBold to true
end if
if not inItalic and runIsItalic then
set end of rowText to "*"
set inItalic to true
end if
-- add the attribute run's text
if my isEmail(runText) then
set end of rowText to "[" & runText & "](mailto:" & runText & ")"
else if my isLink(runText) then
set end of rowText to "[" & runText & "](" & runText & ")"
else
set end of rowText to runText
end if
end repeat
-- close any existing attribute runs
if inBold then
set end of rowText to "**"
set inBold to false
end if
if inItalic then
set end of rowText to "*"
set inItalic to false
end if
-- convert the array of Markdown attribute runs into a single string
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {""}
set markdownText to rowText as text
set AppleScript's text item delimiters to saveTID
end using terms from
return markdownText
end richTextToMarkdown