Skip to content

Commit 6b5fd15

Browse files
committed
feat(open_link): add open link on a tags
1 parent 55afc56 commit 6b5fd15

File tree

8 files changed

+213
-19
lines changed

8 files changed

+213
-19
lines changed

helpers/browser.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os/exec"
7+
"runtime"
8+
)
9+
10+
// OpenBrowser - Open URL in Browser
11+
func OpenBrowser(url string) {
12+
var err error
13+
14+
switch runtime.GOOS {
15+
case "linux":
16+
err = exec.Command("xdg-open", url).Start()
17+
case "windows":
18+
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
19+
case "darwin":
20+
err = exec.Command("open", url).Start()
21+
default:
22+
err = fmt.Errorf("unsupported platform")
23+
}
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
}

main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ import (
99
"strings"
1010

1111
"github.com/darkcl/Notorious/models"
12+
"github.com/darkcl/Notorious/helpers"
1213
"github.com/leaanthony/mewn"
1314
webview "github.com/zserge/webview"
1415
)
1516

1617
func handleRPC(w webview.WebView, data string) {
1718
switch {
1819
case strings.HasPrefix(data, "editor.onChange: "):
19-
fmt.Printf("Recieved Editor changes:\n%s\n", strings.TrimPrefix(data, "editor.onChange: "))
20+
fmt.Printf("editor.onChange:\n%s\n", strings.TrimPrefix(data, "editor.onChange: "))
21+
case strings.HasPrefix(data, "openlink: "):
22+
url := strings.TrimPrefix(data, "openlink: ")
23+
fmt.Printf("openlink:\n%s\n", url)
24+
helpers.OpenBrowser(url)
2025
default:
2126
panic("Not Implemented")
2227
}

ui/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"react-dom": "^16.8.6",
2929
"react-markdown": "^4.1.0",
3030
"react-split-pane": "^0.1.87",
31+
"react-syntax-highlighter": "^11.0.2",
3132
"showdown": "^1.9.0",
3233
"styled-components": "^4.3.2"
3334
}

ui/src/components/App.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import * as React from "react";
22
import SplitPane from "react-split-pane";
3-
import * as ReactMarkdown from "react-markdown";
3+
import * as ReactMarkdown from "react-markdown/with-html";
44

55
import "../style/App.css";
66
import { IAppState } from "../interface/IAppState";
77

88
import { Editor } from "./Editor";
9+
import { CodeBlock } from "./CodeBlock";
910

1011
class App extends React.Component<{}, IAppState> {
1112
constructor(props) {
1213
super(props);
1314

1415
this.state = {
15-
markdownSrc: "# Hello World"
16+
markdownSrc: "# Hello World\n\n[google](http://google.com)"
1617
};
1718

1819
this.onMarkdownChange = this.onMarkdownChange.bind(this);
@@ -35,7 +36,11 @@ class App extends React.Component<{}, IAppState> {
3536
/>
3637
</div>
3738
<div className="view-pane">
38-
<ReactMarkdown className="result" source={this.state.markdownSrc} />
39+
<ReactMarkdown
40+
className="result"
41+
source={this.state.markdownSrc}
42+
renderers={{ code: CodeBlock }}
43+
/>
3944
</div>
4045
</SplitPane>
4146
);

ui/src/components/CodeBlock.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from "react";
2+
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
3+
import { solarizedlight } from "react-syntax-highlighter/dist/esm/styles/prism";
4+
5+
interface ICodeBlock {
6+
value;
7+
language;
8+
}
9+
10+
export class CodeBlock extends React.Component<ICodeBlock, {}> {
11+
constructor(props) {
12+
super(props);
13+
}
14+
15+
render() {
16+
const { language, value } = this.props;
17+
return (
18+
<SyntaxHighlighter language={language} style={solarizedlight}>
19+
{value}
20+
</SyntaxHighlighter>
21+
);
22+
}
23+
}

ui/src/index.html

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7-
<title>TypeScript + React</title>
8-
</head>
9-
<body>
10-
<div id="root">
11-
12-
</div>
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>TypeScript + React</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
1311
</body>
14-
</html>
12+
</html>

ui/src/index.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ import "./style/index.css";
55

66
import App from "./components/App";
77

8-
// declare var counter;
8+
declare var external;
99
declare var folder;
1010

1111
const render = () => ReactDOM.render(<App />, document.getElementById("root"));
1212

1313
// counter.render = render;
1414
folder.render = render;
1515

16+
window.onclick = function(e) {
17+
const elem = e.target as Element;
18+
if (elem.localName === "a") {
19+
e.preventDefault();
20+
external.invoke("openlink: " + elem.getAttribute("href"));
21+
}
22+
};
23+
1624
render();

0 commit comments

Comments
 (0)