@@ -2,6 +2,7 @@ import R from 'ramda'
2
2
3
3
import Prism from 'mastani-codehighlight'
4
4
import { makeDebugger } from '../../utils/functions'
5
+ import network from '../../utils/network'
5
6
6
7
/* eslint-disable no-unused-vars */
7
8
const debug = makeDebugger ( 'L:cheatsheetViewer' )
@@ -37,15 +38,42 @@ export const convertTaskTag = R.compose(
37
38
R . replace ( / < l i > \[ x \] / g, '<li class="task-done">' )
38
39
)
39
40
41
+ const CheatsheetCDN =
42
+ 'https://raw.githubusercontent.com/mydearxym/mastani-cheatsheets/master'
43
+
40
44
export function getData ( which ) {
41
45
setTimeout ( ( ) => {
42
- cheatsheetViewer . SR71$ . getCheatsheet ( which )
46
+ const url = `${ CheatsheetCDN } /${ which } .md`
47
+ network . GET ( url ) . then ( res => {
48
+ /* debug('GET ', res) */
49
+ if ( res . error ) return handleError ( res )
50
+
51
+ let source = ''
52
+ try {
53
+ source = transMarkDownforRender ( res )
54
+ } catch ( err ) {
55
+ return handleError ( { error :'parse_error' } )
56
+ }
57
+ handleLoaded ( source )
58
+ } )
59
+ /* cheatsheetViewer.SR71$.getCheatsheet(which) */
43
60
} , 2000 )
44
61
cheatsheetViewer . markState ( {
45
62
state :'loading' ,
46
63
} )
47
64
}
48
65
66
+ function handleError ( res ) {
67
+ switch ( res . error ) {
68
+ case 404 :
69
+ return handle404Error ( )
70
+ case 'parse_error' :
71
+ return handleParseError ( )
72
+ default :
73
+ debug ( res )
74
+ }
75
+ }
76
+
49
77
function handleParseError ( errMsg ) {
50
78
cheatsheetViewer . markState ( {
51
79
current :'' ,
@@ -55,8 +83,7 @@ function handleParseError(errMsg) {
55
83
Prism . highlightAll ( )
56
84
}
57
85
58
- const is404 = v => R . trim ( v ) === '404: Not Found'
59
- function handle404 ( ) {
86
+ function handle404Error ( ) {
60
87
cheatsheetViewer . markState ( {
61
88
current :'' ,
62
89
state :'404' ,
@@ -75,22 +102,5 @@ function handleLoaded(source) {
75
102
76
103
export function init ( selectedStore ) {
77
104
cheatsheetViewer = selectedStore
78
- // debug('cheatsheetviewer current: ', cheatsheetViewer.current)
79
-
80
- cheatsheetViewer . SR71$ . cheatsheet ( ) . subscribe ( res => {
81
- // console.info('res: ', res)
82
- if ( is404 ( res ) ) {
83
- handle404 ( )
84
- } else {
85
- let source = ''
86
-
87
- try {
88
- source = transMarkDownforRender ( res )
89
- } catch ( err ) {
90
- handleParseError ( err )
91
- return false
92
- }
93
- handleLoaded ( source )
94
- }
95
- } )
105
+ debug ( cheatsheetViewer )
96
106
}