Skip to content

Commit 5db4abe

Browse files
committed
feat(search engine): support bing, duckduckgo, yandex, searx, baidu.
Closes #3
1 parent 3c279f2 commit 5db4abe

File tree

8 files changed

+352
-37
lines changed

8 files changed

+352
-37
lines changed

build.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import fs from 'fs-extra';
77
import process from 'node:process';
88
import tailwindcss from 'tailwindcss';
99
import getManifest from './src/manifest.json.cjs';
10+
import remToPx from 'postcss-rem-to-pixel';
1011

1112
dotenv.config();
1213

@@ -44,7 +45,7 @@ async function runEsbuild() {
4445
plugins: [
4546
postcssPlugin({
4647
postcss: {
47-
plugins: [tailwindcss, autoprefixer],
48+
plugins: [tailwindcss, autoprefixer, remToPx],
4849
},
4950
}),
5051
],

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@emotion/react": "^11.10.6",
1919
"@emotion/styled": "^11.10.6",
2020
"@tailwindcss/typography": "^0.5.9",
21+
"@thedutchcoder/postcss-rem-to-px": "^0.0.2",
2122
"@types/marked": "^4.0.8",
2223
"axios": "^1.3.2",
2324
"conventional-changelog-conventionalcommits": "^5.0.0",
@@ -53,6 +54,7 @@
5354
"file-loader": "^6.2.0",
5455
"fs-extra": "^11.1.0",
5556
"html-loader": "^4.2.0",
57+
"postcss-rem-to-pixel": "^4.1.2",
5658
"prettier": "^2.8.3",
5759
"react-refresh": "^0.14.0",
5860
"react-refresh-typescript": "^2.0.7",

pnpm-lock.yaml

Lines changed: 84 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pages/content/components/LogseqCopliot.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import React from 'react';
22
import { LogseqSearchResult } from '../../../types/logseq-block';
33
import { LogseqResponseType } from '../../logseq/client';
4-
import { ListRender } from './List';
54
import { LogseqBlock } from './LogseqBlock';
65
import { LogseqPageContent } from './LogseqPageContent';
76

87
type LogseqCopliotProps = {
98
connect: chrome.runtime.Port;
10-
hasAside: boolean;
119
};
1210

13-
export const LogseqCopliot = ({ connect, hasAside }: LogseqCopliotProps) => {
11+
export const LogseqCopliot = ({ connect }: LogseqCopliotProps) => {
1412
const [msg, setMsg] = React.useState('Loading...');
1513
const [logseqSearchResult, setLogseqSearchResult] =
1614
React.useState<LogseqSearchResult>();
@@ -108,15 +106,18 @@ export const LogseqCopliot = ({ connect, hasAside }: LogseqCopliotProps) => {
108106
) {
109107
return (
110108
<>
111-
<span>Nothing here, Do some research with Logseq!</span>
109+
<span>
110+
Nothing here, Do some research with Logseq!{' '}
111+
<a href={`logseq://graph/${logseqSearchResult!.graph}`}>Go</a>
112+
</span>
112113
</>
113114
);
114115
}
115116
return <></>;
116117
};
117118

118119
return (
119-
<div id={!hasAside ? 'rhs' : ''} className="copilot">
120+
<div className="copilot">
120121
<div className={msg !== 'success' ? 'content' : 'content divide'}>
121122
{msg !== 'success' ? (
122123
<>{statusShower()}</>
@@ -130,7 +131,7 @@ export const LogseqCopliot = ({ connect, hasAside }: LogseqCopliotProps) => {
130131
)}
131132
</div>
132133

133-
<div className="footer">
134+
<div className="copilot-footer">
134135
<span>
135136
<a href="https://github.com/EINDEX/logseq-copilot/issues/new">
136137
Feedback

src/pages/content/index.scss

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
@tailwind base;
22

33
.copilot {
4-
@apply flex flex-col w-full max-w-full gap-2 pb-4;
4+
@apply flex flex-col w-full max-w-sm gap-2 pb-4;
55

66
.content {
7-
@apply flex flex-col gap-2 rounded-lg border border-gray-300 border-solid px-4 py-4 prose-sm whitespace-pre-wrap;
7+
@apply flex flex-col gap-2 rounded-lg border border-gray-300 border-solid px-4 py-4 prose-sm whitespace-pre-wrap text-sm;
88
* {
9-
@apply max-w-full break-all;
9+
@apply break-all text-sm;
10+
}
11+
12+
pre {
13+
@apply overflow-x-auto;
1014
}
1115

1216
.block-footer {
@@ -43,7 +47,7 @@
4347
.logseq-page-link::after {
4448
content: ']]';
4549
}
46-
.footer {
50+
.copilot-footer {
4751
@apply flex flex-row text-xs justify-between text-gray-500 px-2;
4852

4953
a {

src/pages/content/index.tsx

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
import './index.scss';
22
import { createRoot } from 'react-dom/client';
33
import { LogseqCopliot } from './components/LogseqCopliot';
4+
import searchEngins from './searchingEngines/searchingEngines';
5+
import { Container } from '@chakra-ui/react';
46

57
const connect = chrome.runtime.connect();
68

7-
const mount = async (query: string) => {
8-
const container = document.createElement('div');
9-
const asideElement = document.getElementById('rhs');
10-
11-
const hasAside = !!asideElement;
12-
13-
if (hasAside) {
14-
asideElement.insertBefore(container, asideElement.firstChild);
15-
} else {
16-
const noAsideElement = document.getElementById('rcnt');
17-
noAsideElement!.appendChild(container);
18-
}
19-
9+
const mount = async (container: Element, query: string) => {
2010
const root = createRoot(container);
2111

2212
connect.postMessage({ type: 'query', query: query });
2313

24-
root.render(<LogseqCopliot connect={connect} hasAside={hasAside} />);
14+
root.render(<LogseqCopliot connect={connect} />);
2515
};
2616

27-
async function run() {
17+
async function run(searchEngine) {
2818
console.debug('Logseq copliot', window.location.hostname);
2919

30-
const searchURL = new URL(window.location.href);
31-
const query = searchURL.searchParams.get('q');
32-
if (!query) {
33-
return;
20+
if (searchEngine.isMatch()) {
21+
const query = searchEngine.getQuery();
22+
if (query) {
23+
console.log(`match ${typeof searchEngine}, query ${query}`);
24+
const container = await searchEngine.gotElement();
25+
await mount(container, query);
26+
}
3427
}
28+
}
3529

36-
if (window.location.hostname === 'www.google.com') {
37-
await mount(query);
30+
function getEngine() {
31+
for (const engine of searchEngins) {
32+
if (engine.isMatch()) {
33+
return engine;
34+
}
3835
}
3936
}
4037

41-
run();
38+
const searchEngine = getEngine();
39+
40+
run(searchEngine);
41+
42+
if (searchEngine.reload) {
43+
searchEngine.reload(() => run(searchEngine));
44+
}

0 commit comments

Comments
 (0)