16 |
17 |
18 |
22 |
28 |
29 |
30 |
31 |
32 |
33 |
40 |
41 |
42 |
47 |
48 |
49 |
50 |
{children}
51 | {!home && (
52 | <>
53 |
58 | >
59 | )
60 | }
61 |
62 | );
63 | }
--------------------------------------------------------------------------------
/lib/posts.js:
--------------------------------------------------------------------------------
1 | import fs from 'fs';
2 | import path from 'path';
3 | import matter from 'gray-matter';
4 | import { remark } from 'remark';
5 | import prism from 'remark-prism';
6 | import gfm from 'remark-gfm'
7 | import html from 'remark-html';
8 |
9 | const postsDirectory = path.join(process.cwd(), 'posts');
10 |
11 | export function getSortedPostsData() {
12 | // Get file names under /posts
13 | const fileNames = fs.readdirSync(postsDirectory).filter(s => s.endsWith('_en.md'));
14 | const allPostsData = fileNames.map((fileName) => {
15 | // Remove ".md" from file name to get id
16 | const id = fileName.replace(/\.md$/, '');
17 |
18 | // Read markdown file as string
19 | const fullPath = path.join(postsDirectory, fileName);
20 | const fileContents = fs.readFileSync(fullPath, 'utf8');
21 |
22 | // Use gray-matter to parse the post metadata section
23 | const matterResult = matter(fileContents);
24 |
25 | // Combine the data with the id
26 | return {
27 | id,
28 | ...matterResult.data,
29 | };
30 | });
31 | // Sort posts by date
32 | return allPostsData.sort(({ date: a }, { date: b }) => {
33 | if (a < b) {
34 | return 1;
35 | } else if (a > b) {
36 | return -1;
37 | } else {
38 | return 0;
39 | }
40 | });
41 | }
42 |
43 | export function getAllPostIds() {
44 | const fileNames = fs.readdirSync(postsDirectory);
45 |
46 | // Returns an array that looks like this:
47 | // [
48 | // {
49 | // params: {
50 | // id: 'ssg-ssr'
51 | // }
52 | // },
53 | // {
54 | // params: {
55 | // id: 'pre-rendering'
56 | // }
57 | // }
58 | // ]
59 | return fileNames.map((fileName) => {
60 | return {
61 | params: {
62 | id: fileName.replace(/\.md$/, ''),
63 | },
64 | };
65 | });
66 | }
67 |
68 | export async function getPostData(id) {
69 | const fullPath = path.join(postsDirectory, `${id}.md`);
70 | const fileContents = fs.readFileSync(fullPath, 'utf8');
71 |
72 | // Use gray-matter to parse the post metadata section
73 | const matterResult = matter(fileContents);
74 |
75 | // Use remark to convert markdown into HTML string
76 | const processedContent = await remark()
77 | .use(prism)
78 | .use(gfm)
79 | .use(html, { sanitize: false })
80 | .process(matterResult.content);
81 | const contentHtml = processedContent.toString();
82 |
83 | // Combine the data with the id and contentHtml
84 | return {
85 | id,
86 | contentHtml,
87 | ...matterResult.data,
88 | };
89 | }
--------------------------------------------------------------------------------
/go-code/slice1.gos:
--------------------------------------------------------------------------------
1 | package main
2 |
3 |
4 | func a() bool {
5 | s := []int{8,8}
6 | s[1] -= 9
7 | k := s[1] == -1
8 | return k
9 | }
10 |
11 | func f2() {
12 | var j int
13 | var s = []int{10, 20}
14 | var s2 = []int{100, 200}
15 | for _, v := range s {
16 | j += v
17 | for _, v2 := range s2{
18 | j += v2
19 | for _, v := range s {
20 | j += v
21 | }
22 | }
23 | }
24 | assert(j == 750)
25 | }
26 |
27 |
28 | func slice_slice() {
29 | s := []int{1,2,3,4}
30 | s1 := s[:1]
31 | assert(len(s1) == 1)
32 | assert(cap(s1) == 4)
33 |
34 | s2 := s[1:]
35 | assert(len(s2) == 3)
36 | assert(cap(s2) == 3)
37 |
38 | s3 := s[1:2:2]
39 | assert(s3[0] == 2)
40 | assert(len(s3) == 1)
41 | assert(cap(s3) == 1)
42 |
43 | s4 := s[1:2:3]
44 | assert(cap(s4) == 2)
45 |
46 | // index out of range
47 | //s4 = s[1:2:11]
48 | //assert(cap(s4) == 10)
49 | }
50 |
51 | func append_slice() {
52 | m := []byte{1,3}
53 | n := []byte{2,4}
54 | t := append(m, n...)
55 | assert(t[0] == 1)
56 | assert(t[2] == 2)
57 |
58 | s := "what"
59 | s1 := append(m, s...)
60 | assert(s1[2] == 'w')
61 | assert(s1[3] == 'h')
62 | assert(s1[4] == 'a')
63 | assert(s1[5] == 't')
64 | }
65 |
66 | func copy_slice() {
67 | m := []byte{1,2,3,4}
68 | n := []byte{66,77}
69 | t := m[1:4]
70 | count := copy(t, n)
71 | assert(count == 2)
72 | assert(t[0] == 66)
73 | assert(t[1] == 77)
74 | assert(t[2] == 4)
75 |
76 | t2 := m[:1]
77 | count = copy(t2, n)
78 | assert(count == 1)
79 | assert(t2[0] == 66)
80 | assert(t2[1] == 66)
81 | assert(t2[2] == 77)
82 |
83 | count = copy(t2, "what")
84 | assert(count == 1)
85 | assert(t2[0] == 'w')
86 | assert(t2[1] == 66)
87 | }
88 |
89 |
90 |
91 | func copy_no_return() {
92 | s := "/a"
93 | buf := make([]byte, 3)
94 | copy(buf, s[:1])
95 | assert(buf[0] == '/')
96 | }
97 |
98 |
99 | func appendToNil() {
100 | var a []int
101 | b := []int{6,6,6}
102 | a = append(a, b...)
103 | a[0] = 123
104 | assert(a[0] == 123)
105 | assert(b[0] == 6)
106 | }
107 |
108 |
109 |
110 | func main() {
111 | var s1 = [][]int{{0},{99},{2}}
112 | var s2 = []int{0,100,2}
113 | i := s1[1][0] + s2[1] - 1
114 | s2[0] = 8
115 | j := s2[0]
116 | assert(i == 198)
117 | assert(j == 8)
118 | assert(a())
119 |
120 | f2()
121 |
122 | slice_slice()
123 |
124 | append_slice()
125 |
126 | copy_slice()
127 |
128 | copy_no_return()
129 |
130 | appendToNil()
131 | }
--------------------------------------------------------------------------------
/goscript/src/lib.rs:
--------------------------------------------------------------------------------
1 | use goscript_engine as engine;
2 | use std::borrow::Cow;
3 | use std::io::prelude::*;
4 | use std::io::{Cursor, Result};
5 | use std::path::PathBuf;
6 | use std::sync::{Arc, Mutex};
7 | use wasm_bindgen::prelude::*;
8 |
9 | #[wasm_bindgen]
10 | pub struct RunResult {
11 | out: String,
12 | err: String,
13 | compile_err: String,
14 | debug: String,
15 | }
16 |
17 | #[wasm_bindgen]
18 | impl RunResult {
19 | pub fn new(out: String, err: String, compile_err: String, debug: String) -> RunResult {
20 | RunResult {
21 | out,
22 | err,
23 | compile_err,
24 | debug,
25 | }
26 | }
27 |
28 | #[wasm_bindgen(getter)]
29 | pub fn out(&self) -> String {
30 | self.out.clone()
31 | }
32 |
33 | #[wasm_bindgen(getter)]
34 | pub fn err(&self) -> String {
35 | self.err.clone()
36 | }
37 |
38 | #[wasm_bindgen(getter)]
39 | pub fn compile_err(&self) -> String {
40 | self.compile_err.clone()
41 | }
42 |
43 | #[wasm_bindgen(getter)]
44 | pub fn debug(&self) -> String {
45 | self.debug.clone()
46 | }
47 | }
48 |
49 | #[wasm_bindgen]
50 | pub fn run_zip_and_string(zip: Vec