@@ -18,24 +18,55 @@ async function formatNoteBody(url) {
1818
1919 // remove images
2020 const el = wrapper . querySelector ( 'div' )
21+
2122 if ( el ) {
2223 for ( let img of el . querySelectorAll ( 'img' ) ) {
2324 console . log ( "removing image: " , img . getAttribute ( "alt" ) )
2425 img . remove ( )
2526 }
26- return htmlToMarkdown ( el . toString ( ) )
27+ return {
28+ body : htmlToMarkdown ( el . toString ( ) ) ,
29+ source : getSourceLink ( el )
30+ }
2731 }
28- return "" ;
32+ return { body : "" , source : "" }
2933 } )
3034 . catch ( function ( err ) {
3135 console . error ( "formatNoteBody()" , err )
32- return ""
36+ return { body : "" , source : "" }
3337 } )
3438
3539 return body
3640}
3741
38- async function structureMessage ( note , titlePrefix , isLogin = false ) {
42+ /**
43+ * Get the source link from the note body
44+ * search el for text "Clipped from: " or "Source: "
45+ * if found, select the first href after that location
46+ *
47+ * @param {* } el
48+ * @returns
49+ */
50+ const getSourceLink = ( el ) => {
51+ let text = el . innerText ;
52+ let clippedFromIndex = text . indexOf ( 'Clipped from: ' ) ;
53+ let sourceIndex = text . indexOf ( 'Source: ' ) ;
54+
55+ let link ;
56+ let links = el . querySelectorAll ( 'a' ) ;
57+ for ( let i = 0 ; i < links . length ; i ++ ) {
58+ let linkPosition = text . indexOf ( links [ i ] . innerText ) ;
59+ if ( ( clippedFromIndex !== - 1 && clippedFromIndex < linkPosition ) ||
60+ ( sourceIndex !== - 1 && sourceIndex < linkPosition ) ) {
61+ link = links [ i ] . getAttribute ( 'href' ) ;
62+ break ;
63+ }
64+ }
65+ return link ? `Source: ${ htmlToMarkdown ( link ) } ` : "" ;
66+ }
67+
68+ async function structureMessage ( args ) {
69+ const { note, titlePrefix, isLogin = false } = args ;
3970 if ( isLogin ) {
4071 return {
4172 title : `Login` ,
@@ -53,14 +84,16 @@ async function structureMessage(note, titlePrefix, isLogin = false) {
5384
5485 const imageUrl = links && links . previewImageUrl ? links . previewImageUrl . href : null
5586
56- const body = await formatNoteBody ( url ) ;
87+ const content = await formatNoteBody ( url ) ;
5788
58- const isMsgTooLong = ( titlePrefix . length + title . length + body . length ) > 4096 + 22
89+ const isMsgTooLong = ( titlePrefix . length + title . length + content . body . length ) > 4096 + 22
90+
91+ const previewBody = markdownv2 . escape ( previewText ) + markdownv2 . escape ( `\n\n` ) + content . source
5992
6093 return {
6194 prefix : titlePrefix ,
6295 title,
63- body : isMsgTooLong ? previewText : body ,
96+ body : isMsgTooLong ? previewBody : content . body ,
6497 type : 'link' ,
6598 url : noteLinks [ oneNoteAppUrl ] . href ,
6699 webUrl : noteLinks . oneNoteWebUrl . href ,
@@ -76,7 +109,7 @@ async function structureMessage(note, titlePrefix, isLogin = false) {
76109 * @param {* } login - format note as device login dialog
77110 */
78111function push ( note , icon = '🔒' , login = false ) {
79- const msg = structureMessage ( note , icon , login )
112+ const msg = structureMessage ( { note, titlePrefix : icon , isLogin : login } )
80113
81114 return new Promise ( ( resolve , reject ) => {
82115 fetch
@@ -105,8 +138,13 @@ function push(note, icon = '🔒', login = false) {
105138 * @param {* } login - format note as device login dialog
106139 */
107140async function withTelegram ( note , messageSettings , login = false ) {
108- const { titlePrefix, channelHandle = process . env . DEFAULT_TELEGRAM_CHANNEL , showEditLink = false , disablePreview = true } = messageSettings
109- const msg = await structureMessage ( note , titlePrefix , login )
141+ const {
142+ titlePrefix,
143+ channelHandle = process . env . DEFAULT_TELEGRAM_CHANNEL ,
144+ showEditLink = false ,
145+ disablePreview = true ,
146+ } = messageSettings
147+ const msg = await structureMessage ( { note, titlePrefix, isLogin : login } )
110148
111149 let text = markdownv2 . bold ( markdownv2 . escape ( msg . prefix ) )
112150 text += markdownv2 . escape ( `\n\n` )
0 commit comments