Skip to content

Commit b2a406f

Browse files
authored
Merge pull request #10 from imvladikon/migrate/v3
Migrate/v3
2 parents 2dc49ca + ca50a88 commit b2a406f

File tree

95 files changed

+18689
-1058
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+18689
-1058
lines changed

Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Build extension for Chrome Web Store
2+
zip:
3+
zip -r jupyter-notebook-viewer.zip . \
4+
-x "*.DS_Store" \
5+
-x "node_modules/*" \
6+
-x ".git/*" \
7+
-x ".idea/*" \
8+
-x "test/*" \
9+
-x "*.test.json" \
10+
-x "package-lock.json" \
11+
-x ".gitignore" \
12+
-x ".gitmodules" \
13+
-x "Makefile" \
14+
-x "debug-cli/*"
15+
16+
# Clean build artifacts
17+
clean:
18+
rm -f jupyter-notebook-viewer.zip
19+
rm -rf node_modules/.cache
20+
21+
# Install dependencies
22+
install:
23+
npm install
24+
25+
# Run tests
26+
test:
27+
npm test
28+
29+
# Build all components
30+
build: build-mdc build-prism build-remark build-themes
31+
32+
build-mdc:
33+
npm run build:mdc
34+
35+
build-prism:
36+
npm run build:prism
37+
38+
build-remark:
39+
npm run build:remark
40+
41+
build-themes:
42+
npm run build:themes
43+
44+
# Development workflow
45+
dev: install build
46+
47+
# Package for store submission
48+
package: clean build zip
49+
50+
.PHONY: zip clean install test build build-mdc build-prism build-remark build-themes dev package

background/compilers/remark.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ md.compilers.remark = (() => {
1616
var ctor = ({storage: {state}}) => ({
1717
defaults,
1818
description,
19-
compile: (markdown) =>
20-
remark.unified()
21-
.use(remark.parse)
22-
.use(state.remark.gfm ? remark.gfm : undefined)
23-
.use(state.remark.breaks ? remark.breaks : undefined)
24-
.use(state.remark.footnotes ? remark.footnotes : undefined)
25-
.use(remark.stringify)
26-
.use(remark.slug)
27-
.use(remark.frontmatter, ['yaml', 'toml'])
28-
.use(remark.html, state.remark) // sanitize
29-
.processSync(markdown)
30-
.contents
19+
compile: (markdown) => {
20+
// Remark v15+ API - using unified processor
21+
const processor = remark.unified()
22+
.use(remark.remarkParse)
23+
.use(state.remark.gfm ? remark.remarkGfm : undefined)
24+
.use(state.remark.breaks ? remark.remarkBreaks : undefined)
25+
.use(state.remark.footnotes ? remark.remarkFootnotes : undefined)
26+
.use(remark.remarkSlug)
27+
.use(remark.remarkFrontmatter, ['yaml', 'toml'])
28+
.use(remark.remarkHtml, state.remark) // sanitize
29+
30+
return processor.processSync(markdown).toString()
31+
}
3132
})
3233

3334
return Object.assign(ctor, {defaults, description})

background/detect.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
md.detect = ({storage: {state}, inject}) => {
1+
// Clean detection implementation based on markdown-viewer patterns
2+
// Simplified detection logic with better error handling
23

4+
md.detect = ({storage: {state}, inject}) => {
5+
36
var onwakeup = true
4-
5-
var code = `
6-
JSON.stringify({
7-
url: window.location.href,
8-
header: document.contentType,
9-
loaded: !!window.state,
10-
})
11-
`
12-
7+
138
var tab = (id, info, tab) => {
149
if (info.status === 'loading') {
15-
// try
16-
chrome.tabs.executeScript(id, {code, runAt: 'document_start'}, (res) => {
10+
11+
// Execute detection script
12+
chrome.scripting.executeScript({
13+
target: {tabId: id},
14+
func: () =>
15+
JSON.stringify({
16+
url: window.location.href,
17+
header: document.contentType,
18+
loaded: !!window.state || !!window.args,
19+
})
20+
}, (res) => {
1721
if (chrome.runtime.lastError) {
18-
// origin not allowed
22+
// Origin not allowed or other error
1923
return
2024
}
21-
25+
2226
try {
23-
var win = JSON.parse(res)
27+
var win = JSON.parse(res[0].result)
28+
if (!win) return
2429
} catch (err) {
25-
// JSON parse error
2630
return
2731
}
28-
32+
2933
if (win.loaded) {
30-
// anchor
31-
return
34+
// Already processed
35+
return
3236
}
33-
34-
if (header(win.header) || match(win.url)) {
37+
38+
if (detect(win.header, win.url)) {
3539
if (onwakeup && chrome.webRequest) {
3640
onwakeup = false
3741
chrome.tabs.reload(id)
@@ -42,25 +46,30 @@ md.detect = ({storage: {state}, inject}) => {
4246
})
4347
}
4448
}
45-
46-
var header = (value) => {
47-
return state.header && value && /text\/(?:x-)?markdown/i.test(value)
48-
}
49-
50-
var match = (url) => {
49+
50+
var detect = (content, url) => {
5151
var location = new URL(url)
52-
53-
var origin =
52+
53+
// Check if URL ends with .ipynb
54+
if (!location.pathname.endsWith('.ipynb')) {
55+
return false
56+
}
57+
58+
// Find matching origin
59+
var origin =
5460
state.origins[location.origin] ||
5561
state.origins[location.protocol + '//' + location.hostname] ||
56-
state.origins['*://' + location.host] ||
5762
state.origins['*://' + location.hostname] ||
63+
state.origins['*://' + location.host] ||
5864
state.origins['*://*']
59-
60-
if (origin && origin.match && new RegExp(origin.match).test(location.href)) {
61-
return origin
65+
66+
// Default file:// support for .ipynb files
67+
if (!origin && location.protocol === 'file:') {
68+
origin = state.origins['file://'] || {match: state.match}
6269
}
70+
71+
return origin && new RegExp(origin.match || state.match).test(url)
6372
}
64-
65-
return {tab, header, match}
66-
}
73+
74+
return {tab}
75+
}

background/inject.js

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,59 @@
1+
// Clean injection implementation based on markdown-viewer patterns
2+
// Conditional script injection and simplified logic
3+
14
md.inject = ({storage: {state}}) => (id) => {
5+
6+
console.log('[Inject] Injecting into tab', id, 'with state:', {
7+
theme: state.theme,
8+
content: state.content
9+
})
210

3-
chrome.tabs.executeScript(id, {
4-
code: `
11+
chrome.scripting.executeScript({
12+
target: {tabId: id},
13+
args: [{
14+
theme: state.theme,
15+
raw: false, // Force false for notebook rendering
16+
themes: state.themes,
17+
content: state.content,
18+
compiler: state.compiler,
19+
}],
20+
func: (_args) => {
521
document.querySelector('pre').style.visibility = 'hidden'
6-
var theme = ${JSON.stringify(state.theme)}
7-
var raw = ${state.raw}
8-
var themes = ${JSON.stringify(state.themes)}
9-
var content = ${JSON.stringify(state.content)}
10-
var compiler = '${state.compiler}'
11-
`,
12-
runAt: 'document_start'
22+
window.args = _args
23+
},
24+
injectImmediately: true
1325
})
1426

15-
chrome.tabs.insertCSS(id, {file: 'content/index.css', runAt: 'document_start'})
16-
chrome.tabs.insertCSS(id, {file: 'vendor/prism.min.css', runAt: 'document_start'})
17-
chrome.tabs.insertCSS(id, {
18-
file: 'https://cdn.jsdelivr.net/npm/katex@0.15.3/dist/katex.min.css',
19-
runAt: 'document_start'
27+
var cssFiles = [
28+
'/content/index.css',
29+
'/vendor/prism.min.css',
30+
'/vendor/katex.min.css',
31+
state.theme && `/themes/${state.theme}.css`,
32+
].filter(Boolean)
33+
34+
console.log('[Inject] Inserting CSS files:', cssFiles)
35+
36+
chrome.scripting.insertCSS({
37+
target: {tabId: id},
38+
files: cssFiles
2039
})
2140

22-
chrome.tabs.executeScript(id, {file: 'vendor/mithril.min.js', runAt: 'document_start'})
23-
chrome.tabs.executeScript(id, {file: 'vendor/es5-shim.min.js', runAt: 'document_start'})
24-
chrome.tabs.executeScript(id, {file: 'vendor/marked.min.js', runAt: 'document_start'})
25-
chrome.tabs.executeScript(id, {file: 'vendor/ansi_up.min.js', runAt: 'document_start'})
26-
chrome.tabs.executeScript(id, {file: 'vendor/prism.min.js', runAt: 'document_start'})
27-
chrome.tabs.executeScript(id, {
28-
file: 'https://cdn.jsdelivr.net/npm/katex@0.15.3/dist/katex.min.js',
29-
runAt: 'document_start'
41+
chrome.scripting.executeScript({
42+
target: {tabId: id},
43+
files: [
44+
'/vendor/mithril.min.js',
45+
'/vendor/marked.min.js',
46+
'/vendor/ansi_up.min.js',
47+
'/vendor/katex.min.js',
48+
'/vendor/katex-auto-render.min.js',
49+
'/vendor/notebook.min.js',
50+
state.content.syntax && '/vendor/prism.min.js',
51+
state.content.emoji && '/content/emoji.js',
52+
state.content.mathjax && ['/vendor/mathjax/tex-chtml.min.js', '/content/mathjax.js'],
53+
'/content/index.js',
54+
state.content.autoreload && '/content/autoreload.js',
55+
].filter(Boolean).flat(),
56+
injectImmediately: true
3057
})
31-
chrome.tabs.executeScript(id, {
32-
file: 'https://cdn.jsdelivr.net/npm/katex@0.15.3/dist/contrib/auto-render.min.js',
33-
runAt: 'document_start'
34-
})
35-
chrome.tabs.executeScript(id, {file: 'vendor/notebook.min.js', runAt: 'document_start'})
36-
3758

38-
if (state.content.emoji) {
39-
chrome.tabs.executeScript(id, {file: 'content/emoji.js', runAt: 'document_start'})
40-
}
41-
chrome.tabs.executeScript(id, {file: 'content/index.js', runAt: 'document_start'})
42-
}
59+
}

background/messages.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
md.messages = ({storage: {defaults, state, set}, compilers, mathjax, xhr, webrequest}) => {
1+
md.messages = ({storage, compilers, mathjax, xhr, webrequest}) => {
2+
// Extract storage properties
3+
const {defaults, state, set} = storage || {}
24

35
return (req, sender, sendResponse) => {
46

57
// content
6-
if (req.message === 'markdown') {
8+
if (req.message === 'ping') {
9+
sendResponse({message: 'pong', status: 'ok', extension: 'Jupyter Notebook Viewer'})
10+
} else if (req.message === 'get-config') {
11+
sendResponse({
12+
config: {
13+
theme: state.theme,
14+
raw: state.raw,
15+
themes: state.themes,
16+
content: state.content,
17+
compiler: state.compiler
18+
}
19+
})
20+
} else if (req.message === 'markdown') {
721
var markdown = req.markdown
822

923
if (state.content.mathjax) {
@@ -19,10 +33,18 @@ md.messages = ({storage: {defaults, state, set}, compilers, mathjax, xhr, webreq
1933

2034
sendResponse({message: 'html', html})
2135
} else if (req.message === 'nbjson') {
36+
console.log('[Messages] Processing nbjson request');
37+
console.log('[Messages] Current compiler:', state.compiler);
38+
console.log('[Messages] Available compilers:', compilers ? Object.keys(compilers) : 'none');
39+
console.log('[Messages] Notebook cells:', req.nbjson && req.nbjson.cells ? req.nbjson.cells.length : 0);
2240

2341
var nbjson = req.nbjson
2442
sendResponse({message: 'html', nbjson: nbjson})
2543

44+
} else if (req.message === 'mathjax') {
45+
// MathJax extension loading removed - using minimal build
46+
console.log('[Messages] MathJax extension request ignored (using minimal build)');
47+
sendResponse();
2648
} else if (req.message === 'autoreload') {
2749
xhr.get(req.location, (err, body) => {
2850
sendResponse({err, body})
@@ -38,7 +60,9 @@ md.messages = ({storage: {defaults, state, set}, compilers, mathjax, xhr, webreq
3860
themes: state.themes,
3961
}))
4062
} else if (req.message === 'popup.theme') {
63+
console.log('[Background] Theme change from popup:', req.theme)
4164
set({theme: req.theme})
65+
console.log('[Background] Notifying content script of theme change')
4266
notifyContent({message: 'theme', theme: req.theme})
4367
sendResponse()
4468
} else if (req.message === 'popup.raw') {

0 commit comments

Comments
 (0)