@@ -49,9 +49,29 @@ def wrap_text_with_index(self, content):
4949 final_lines .append (f"{ line } ({ i } /{ len (wrapped_lines )} )" )
5050 return final_lines
5151
52+ def build_organization_mentions (self , mentions ):
53+ output = []
54+ for mention in mentions :
55+ vanity_name = mention .strip ()
56+ urn = None
57+ try :
58+ response = requests .get (
59+ f"{ self .api_base_url } /organizations" ,
60+ headers = self .headers ,
61+ params = {"q" : "vanityName" , "vanityName" : vanity_name },
62+ )
63+ response .raise_for_status ()
64+ elements = response .json ().get ("elements" , [])
65+ if elements and (org_id := elements [0 ].get ("id" )):
66+ urn = f"urn:li:organization:{ org_id } "
67+ mention = elements [0 ].get ("localizedName" )
68+ except Exception as e :
69+ print (f"[WARN] Failed to resolve @{ mention } : { e } " )
70+ output .append (f"@[{ mention } ]({ urn } )" if urn else f"@{ mention } " )
71+ return " " .join (output )
72+
5273 def format_content (self , content , mentions , hashtags , images , ** kwargs ):
53- # the mentions are not linked to anyone!
54- mentions = " " .join ([f"@{ v } " for v in mentions ])
74+ mentions = self .build_organization_mentions (mentions )
5575 hashtags = " " .join ([f"#{ v } " for v in hashtags ])
5676 if len (images ) > 20 :
5777 warnings = f"A maximum of 20 images, not { len (images )} , can be included in a single linkedin post."
@@ -60,11 +80,23 @@ def format_content(self, content, mentions, hashtags, images, **kwargs):
6080 warnings = ""
6181
6282 # convert markdown formatting because linkedin doesn't support it
83+ protected_mentions = {}
84+
85+ def protect (match ):
86+ key = f"PROTECTED_MENTION_{ len (protected_mentions )} "
87+ protected_mentions [key ] = match .group (0 )
88+ return key
89+
90+ content = re .sub (r"@\[[^\]]+\]\(urn:li:organization:\d+\)" , protect , content )
91+
6392 paragraphs = content .split ("\n \n \n " )
6493 for i , p in enumerate (paragraphs ):
6594 paragraphs [i ] = strip_markdown_formatting (p )
6695 content = "\n \n \n " .join (paragraphs )
6796
97+ for key , value in protected_mentions .items ():
98+ content = content .replace (key , value )
99+
68100 content += "\n "
69101 if mentions :
70102 content = f"{ content } \n { mentions } "
0 commit comments