1
1
import { nanoid } from 'nanoid' ;
2
2
3
3
import GistPluginSettings from './settings' ;
4
- import { request , RequestUrlParam } from "obsidian" ;
4
+ import { request , RequestUrlParam } from "obsidian" ;
5
5
6
6
type GistJSON = {
7
7
description : string ,
@@ -96,6 +96,7 @@ class GistProcessor {
96
96
container . classList . add ( `${ pluginName } -container` )
97
97
container . setAttribute ( 'sandbox' , 'allow-scripts allow-top-navigation-by-user-activation' )
98
98
container . setAttribute ( 'loading' , 'lazy' )
99
+ // container.setAttribute('csp', "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://github.githubassets.com;")
99
100
100
101
// reset the default things on HTML
101
102
const resetStylesheet = `
@@ -127,15 +128,13 @@ class GistProcessor {
127
128
</script>
128
129
`
129
130
130
- // build stylesheet link
131
- const stylesheetLink = document . createElement ( 'link' ) ;
132
- stylesheetLink . rel = "stylesheet" ;
133
- stylesheetLink . href = gistJSON . stylesheet
134
-
135
131
// hack to make links open in the parent
136
132
const parentLinkHack = document . createElement ( 'base' )
137
133
parentLinkHack . target = "_parent"
138
134
135
+ // load stylesheet as text
136
+ const stylesheetText = await this . _loadStylesheet ( el , gistID , gistJSON . stylesheet ) ;
137
+
139
138
// custom stylesheet
140
139
let customStylesheet = ""
141
140
if ( this . settings . styleSheet && this . settings . styleSheet . length > 0 ) {
@@ -151,8 +150,10 @@ class GistProcessor {
151
150
${ parentLinkHack . outerHTML }
152
151
${ heightAdjustmentScript }
153
152
154
- <!-- gist style -->
155
- ${ stylesheetLink . outerHTML }
153
+ <!-- gist embedded style -->
154
+ <style>
155
+ ${ stylesheetText }
156
+ </style>
156
157
157
158
<!-- custom style -->
158
159
<style>
@@ -181,6 +182,23 @@ Error:
181
182
182
183
el . createEl ( 'pre' , { text : errorText } )
183
184
}
185
+
186
+ private async _loadStylesheet ( el : HTMLElement , gistString : String , url : string ) {
187
+ const urlParam : RequestUrlParam = {
188
+ url : url ,
189
+ method : "GET" ,
190
+ headers : {
191
+ "Accept" : "text/css"
192
+ }
193
+ }
194
+
195
+ try {
196
+ const res = await request ( urlParam )
197
+ return res ;
198
+ } catch ( error ) {
199
+ return this . _showError ( el , gistString , `Could not fetch the Gist Style from GitHub server. (Error: ${ error } )` )
200
+ }
201
+ }
184
202
}
185
203
186
204
export { GistProcessor }
0 commit comments