8000 Introduced webpack for docs, to support older browsers · galaxyblur/nativescript-vue@408c51b · GitHub
[go: up one dir, main page]

Skip to content

Commit 408c51b

Browse files
committed
Introduced webpack for docs, to support older browsers
1 parent 69c17bc commit 408c51b

Some content is hidden

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

49 files changed

+232
-176
lines changed

docs/index.html

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -87,75 +87,23 @@
8787
</head>
8888
<body>
8989
<div id="app"></div>
90-
<script src="https://unpkg.com/marked@0.3.6/lib/marked.js"></script>
91-
<script src="https://unpkg.com/docute@3/dist/docute.js"></script>
92-
<script>
93-
((og_fetch) => {
94-
fetch = (...args) => {
95-
const promises = [
96-
() => og_fetch.apply(this, args),
97-
...fetch.interceptors
98-
]
99-
return promises.reduce((prev, curr) => {
100-
return prev.then(curr)
101-
}, Promise.resolve())
102-
}
10390

104-
fetch.interceptors = []
105-
})
106-
(fetch)
107-
</script>
108-
<script src="nsdocs.js"></script>
109-
<script>
110-
docute.init({
111-
debug: true,
112-
113-
nav: [
114-
{
115-
title: 'Roadmap',
116-
path: 'https://trello.com/b/mOdP0SHD/nativescript-vue-roadmap',
117-
},
118-
],
119-
120-
icons: [
121-
{
122-
icon: 'github',
123-
label: 'Contribute on GitHub',
124-
link: 'https://github.com/rigor789/nativescript-vue',
125-
},
126-
],
127-
128-
toc: './_toc.md',
129-
130-
plugins: [
131-
({ registerComponent }) => {
132-
registerComponent('sidebar:start', {
133-
template: `
134-
<div class="Logo" router-link="/">
135-
<img src="https://art.nativescript-vue.org/NativeScript-Vue-Green-White.png" alt="NativeScript-Vue">
136-
<img src="https://art.nativescript-vue.org/NativeScript-Vue-White-Green.png" alt="NativeScript-Vue">
137-
</div>
138-
`,
139-
})
140-
},
141-
NSDocs()
142-
],
143-
})
144-
</script>
91+
<script src="https://unpkg.com/docute/dist/docute.js"></script>
92+
<script src="dist/docs.js"></script>
93+
14594
<script>
146-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
95+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
14796
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
14897
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
149-
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
98+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
15099

151-
ga('create', 'UA-106632905-1', 'auto');
152-
ga('send', 'pageview');
100+
ga('create', 'UA-106632905-1', 'auto');
101+
ga('send', 'pageview');
153102

154-
docute.router.afterEach((to) => {
155-
ga('set', 'page', to.fullPath)
156-
ga('send', 'pageview')
157-
})
158-
159-
</script>
103+
docute.router.afterEach((to) => {
104+
ga('set', 'page', to.fullPath)
105+
ga('send', 'pageview')
106+
})
107+
</script>
160108
</body>
161109
</html>

docs/nsdocs.js

Lines changed: 0 additions & 112 deletions
This file was deleted.

docs/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "nativescript-vue-docs",
3+
"version": "1.0.0",
4+
"description": "NativeScript-Vue docs",
5+
"scripts": {
6+
"build": "webpack",
7+
"dev": "webpack --watch"
8+
},
9+
"dependencies": {
10+
"babel-core": "^6.26.0",
11+
"babel-loader": "^7.1.2",
12+
"babel-polyfill": "^6.26.0",
13+
"babel-preset-env": "^1.6.1",
14+
"docute": "^3.4.9",
15+
"marked": "^0.3.6",
16+
"webpack": "^3.8.1"
17+
}
18+
}

docs/src/FetchInterceptors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function FetchInterceptors(og_fetch) {
2+
fetch = (...args) => {
3+
const promises = [
4+
() => og_fetch.apply(this, args),
5+
...fetch.interceptors
6+
]
7+
return promises.reduce((prev, curr) => {
8+
return prev.then(curr)
9+
}, Promise.resolve())
10+
}
11+
12+
fetch.interceptors = []
13+
14+
return fetch;
15+
}

docs/src/main.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { FetchInterceptors } from "./FetchInterceptors";
2+
import { SidebarLogo } from "./plugins/SidebarLogo";
3+
import { ExtendDocs } from "./plugins/ExtendDocs";
4+
5+
// add interceptors to fetch
6+
window.fetch = FetchInterceptors(window.fetch)
7+
8+
docute.init({
9+
debug: true,
10+
11+
url: './markdown/',
12+
toc: './markdown/_toc.md',
13+
14+
nav: [
15+
{
16+
title: 'Roadmap',
17+
path: 'https://trello.com/b/mOdP0SHD/nativescript-vue-roadmap',
18+
},
19+
],
20+
21+
icons: [
22+
{
23+
icon: 'github',
24+
label: 'Contribute on GitHub',
25+
link: 'https://github.com/rigor789/nativescript-vue',
26+
},
27+
],
28+
29+
plugins: [
30+
SidebarLogo,
31+
ExtendDocs()
32+
],
33+
})

docs/src/plugins/ExtendDocs.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import marked from 'marked';
2+
3+
export function ExtendDocs() {
4+
fetch.interceptors.push(async (res) => {
5+
if (res.url.endsWith('.md')) {
6+
const requested_markdown = await res.clone().text()
7+
const original = requested_markdown.match(/original: ?(.+)/)
8+
9+
if (original) {
10+
const final = await process(requested_markdown, original[1])
11+
12+
res.text = () => Promise.resolve(final)
13+
}
14+
}
15+
return res;
16+
})
17+
}
18+
19+
20+
async function process(fetched_markdown, original_url) {
21+
const original_markdown_res = await fetch(original_url)
22+
let original_markdown = await original_markdown_res.text()
23+
24+
fetched_markdown = parseMarkdownContent(fetched_markdown)
25+
original_markdown = parseMarkdownContent(original_markdown)
26+
27+
const json = getCodes(fetched_markdown.markdown).find(({ lang }) => lang === 'json')
28+
const rules = Object.assign({ rename: {}, remove: [] }, json ? JSON.parse(json.code) : {})
29+
const headings = getHeadings(original_markdown.markdown)
30+
31+
headings.forEach(({ title, level }, index) => {
32+
const markdown_title = `${'#'.repeat(level)} ${title}`
33+
34+
if (rules.rename.hasOwnProperty(markdown_title)) {
35+
// we want to rename a title
36+
const renamed_title = rules.rename[markdown_title]
37+
original_markdown.markdown = original_markdown.markdown.replace(markdown_title, renamed_title)
38+
}
39+
40+
if (rules.remove.indexOf(markdown_title) !== -1) {
41+
// we want to remove a section
42+
const startIndex = original_markdown.markdown.search(markdown_title)
43+
let endIndex = original_markdown.markdown.length
44+
45+
if (index !== headings.length - 1) {
46+
const next_heading = headings[index + 1]
47+
const next_heading_markdown = `${'#'.repeat(next_heading.level)} ${next_heading.title}`
48+
49+
endIndex = original_markdown.markdown.search(next_heading_markdown)
50+
}
51+
52+
const to_remove = original_markdown.markdown.substring(startIndex, endIndex)
53+
54+
original_markdown.markdown = original_markdown.markdown.replace(to_remove, '')
55+
}
56+
})
57+
58+
if (json) {
59+
fetched_markdown.markdown = fetched_markdown.markdown.replace('```json\n' + json.code + '\n```', '')
60+
}
61+
62+
return `${original_markdown.frontMatter}
63+
<p class="tip">
64+
This page has been auto generated based on [the official NativeScript Documentation](${original_url})
65+
</p>
66+
${original_markdown.markdown}
67+
${fetched_markdown.markdown}`
68+
}
69+
70+
/**
71+
* Extract headings from markdown string
72+
*/
73+
function getHeadings(markdown) {
74+
const headings = []
75+
76+
const renderer = new marked.Renderer()
77+
renderer.heading = (title, level) => headings.push({ title, level })
78+
79+
marked(markdown, {
80+
renderer: renderer
81+
})
82+
83+
return headings
84+
}
85+
86+
/**
87+
* Extract code blocks from markdown string
88+
*/
89+
function getCodes(markdown) {
90+
const codes = []
91+
92+
const renderer = new marked.Renderer()
93+
renderer.code = (code, lang) => codes.push({ code, lang })
94+
95+
marked(markdown, {
96+
renderer: renderer
97+
})
98+
99+
return codes
100+
}
101+
102+
/**
103+
* Parse fetched markdown file and remove / extract front matter
104+
*/
105+
function parseMarkdownContent(raw) {
106+
const markdown = raw.replace(/^---[\s\S]+?---/g, '')
107+
const frontMatter = raw.substring(0, raw.length - markdown.length)
108+
109+
return {
110+
markdown,
111+
frontMatter
112+
}
113+
}

docs/src/plugins/SidebarLogo.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function SidebarLogo({ registerComponent }) {
2+
registerComponent('sidebar:start', {
3+
template: `
4+
<div class="Logo" router-link="/">
5+
<img src="https://art.nativescript-vue.org/NativeScript-Vue-Green-White.png" alt="NativeScript-Vue">
6+
<img src="https://art.nativescript-vue.org/NativeScript-Vue-White-Green.png" alt="NativeScript-Vue">
7+
</div>
8+
`,
9+
})
10+
}

0 commit comments

Comments
 (0)
0