-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robert Masen
committed
Jun 26, 2018
1 parent
3fd60a2
commit b057fdd
Showing
14 changed files
with
323 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "node-and-browser" | ||
version = "0.1.0" | ||
authors = ["Robert Masen <[email protected]>"] | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
# Here we're using a path dependency to use what's already in this repository, | ||
# but you'd use the commented out version below if you're copying this into your | ||
# project. | ||
wasm-bindgen = { path = "../.." } | ||
#wasm-bindgen = "0.2" | ||
[dependencies.pulldown-cmark] | ||
version = "0.1.2" | ||
default-features = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Node and Browser | ||
|
||
|
||
This directory is an example of using the `#[wasm_bindgen]` macro to create an | ||
entry point that's callable from both the browser and Node.js. | ||
|
||
You can build the example locally with: | ||
|
||
``` | ||
$ ./build.sh | ||
``` | ||
This will build the project and spin up `webpack-dev-server` which makes it viewable at http://localhost:8080 | ||
(or a higher port if 8080 is already bound). | ||
|
||
To see this project used in Node.js you can exit the instance of `webpack-dev-server` and run | ||
|
||
``` | ||
$ npm start | ||
``` | ||
|
||
This will read in the `wasm-bindgen` (README.md)[../../README.md] file and convert it to HTML | ||
you can find the results in `./out.html` once it is finished. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/sh | ||
|
||
set -ex | ||
|
||
# Build the `hello_world.wasm` file using Cargo/rustc | ||
cargo +nightly build --target wasm32-unknown-unknown | ||
|
||
# Run the `wasm-bindgen` CLI tool to postprocess the wasm file emitted by the | ||
# Rust compiler to emit the JS support glue that's necessary | ||
# | ||
# Here we're using the version of the CLI in this repository, but for external | ||
# usage you'd use the commented out version below | ||
cargo +nightly run --manifest-path ../../crates/cli/Cargo.toml --bin wasm-bindgen -- ../../target/wasm32-unknown-unknown/debug/node_and_browser.wasm --out-dir . --both | ||
# wasm-bindgen ../../target/wasm32-unknown-unknown/hello_world.wasm --out-dir . | ||
|
||
# Finally, package everything up using Webpack and start a server so we can | ||
# browse the result | ||
npm install | ||
npm run serve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<html> | ||
<head> | ||
<style> | ||
html, | ||
body { | ||
width: 100%; | ||
height: 100%; | ||
padding: 0; | ||
border: 0; | ||
margin: 0; | ||
} | ||
* { | ||
box-sizing: border-box; | ||
} | ||
#app-container { | ||
width: calc(100% - 20px); | ||
height: calc(100% - 20px); | ||
padding: 10px; | ||
background: slategray; | ||
display: flex; | ||
flex-flow: row; | ||
justify-content: space-between; | ||
align-items: flex-start; | ||
align-content: flex-start; | ||
} | ||
#md-container, | ||
#html-container { | ||
width: calc(50% - 10px); | ||
height: 100%; | ||
padding: 0 5px; | ||
} | ||
#html-container { | ||
background: white; | ||
} | ||
#md-input-box { | ||
resize: none; | ||
width: 100%; | ||
height: 100%; | ||
} | ||
</style> | ||
<link href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/2.10.0/github-markdown.min.css" rel="stylesheet" type="text/css" /> | ||
</head> | ||
<body> | ||
<div id="app-container"> | ||
<div id="md-container"> | ||
<textarea id="md-input-box"></textarea> | ||
</div> | ||
<div id="html-container" class="markdown-body"> | ||
<div id="rendered"></div> | ||
</div> | ||
</div> | ||
<script src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
let wasm; | ||
import('./node_and_browser.js').then(w => { | ||
console.log('wasm_cmark_parse resolved'); | ||
wasm = w; | ||
ready(); | ||
}); | ||
|
||
function parseMD(md) { | ||
console.log('parseMD'); | ||
return wasm.parse_markdown(md); | ||
} | ||
|
||
let debounce; | ||
function ready() { | ||
console.log('ready'); | ||
renderMD(); | ||
let input = getInput(); | ||
if (!input) throw new Error('Unable to find MD input'); | ||
input.addEventListener('keyup', () => { | ||
if (!debounce) { | ||
debounce = setTimeout(renderMD, 2000); | ||
} else { | ||
clearTimeout(debounce); | ||
debounce = setTimeout(renderMD, 2000); | ||
} | ||
}); | ||
} | ||
function renderMD() { | ||
console.log('renderMD'); | ||
debounce = null; | ||
let input = getInput(); | ||
if (!input) throw new Error('Unable to find MD input'); | ||
let md = input.value; | ||
let html = parseMD(md); | ||
let target = getHTML(); | ||
if (!target) throw new Error('Unable to find div to render HTML'); | ||
target.innerHTML = html; | ||
} | ||
|
||
function getInput() { | ||
console.log('getInput'); | ||
return document.getElementById('md-input-box'); | ||
} | ||
|
||
function getHTML() { | ||
console.log('getHTML'); | ||
return document.getElementById('rendered'); | ||
} |
Oops, something went wrong.