-
-
Notifications
You must be signed in to change notification settings - Fork 748
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
Showing
10 changed files
with
319 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { FC } from 'react' | ||
import { ViewPorps } from '@/components/dictionaries/helpers' | ||
import { JikipediaResult } from './engine' | ||
|
||
export const Jikipedia: FC<ViewPorps<JikipediaResult>> = ({ result }) => ( | ||
<ul className="dictJikipedia-List"> | ||
{result.map(item => ( | ||
<li key={item.title + item.url} className="dictJikipedia-Item"> | ||
<h2 className="dictJikipedia-Title"> | ||
<a href={item.url} target="_blank" rel="nofollow noopener noreferrer"> | ||
{item.title} | ||
</a> | ||
</h2> | ||
{item.content && ( | ||
<div | ||
className="dictJikipedia-Content" | ||
dangerouslySetInnerHTML={{ __html: item.content }} | ||
/> | ||
)} | ||
<footer className="dictJikipedia-Footer"> | ||
{item.author && ( | ||
<a | ||
className="dictJikipedia-Author" | ||
href={item.author.url} | ||
target="_blank" | ||
rel="nofollow noopener noreferrer" | ||
> | ||
{item.author.name} | ||
</a> | ||
)} | ||
{item.likes > 0 && ( | ||
<span className="dictJikipedia-Thumbs"> | ||
<svg | ||
className="dictJikipedia-IconThumbsUp" | ||
width="0.9em" | ||
height="0.9em" | ||
fill="#666" | ||
viewBox="0 0 561 561" | ||
> | ||
<path d="M0 535.5h102v-306H0v306zM561 255c0-28.05-22.95-51-51-51H349.35l25.5-117.3v-7.65c0-10.2-5.1-20.4-10.2-28.05L336.6 25.5 168.3 193.8c-10.2 7.65-15.3 20.4-15.3 35.7v255c0 28.05 22.95 51 51 51h229.5c20.4 0 38.25-12.75 45.9-30.6l76.5-181.05c2.55-5.1 2.55-12.75 2.55-17.85v-51H561c0 2.55 0 0 0 0z" /> | ||
</svg> | ||
{item.likes} | ||
</span> | ||
)} | ||
</footer> | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
|
||
export default Jikipedia |
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 @@ | ||
{ | ||
"name": { | ||
"en": "Jikipedia", | ||
"zh-CN": "小鸡词典", | ||
"zh-TW": "小雞詞典" | ||
}, | ||
"options": { | ||
"resultnum": { | ||
"en": "Show", | ||
"zh-CN": "结果数量", | ||
"zh-TW": "結果數量" | ||
}, | ||
"resultnum_unit": { | ||
"en": "results", | ||
"zh-CN": "个", | ||
"zh-TW": "個" | ||
} | ||
} | ||
} |
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,83 @@ | ||
.brax-render { | ||
display: block | ||
} | ||
|
||
.brax-node { | ||
word-wrap: break-word | ||
} | ||
|
||
.brax-bold,.braxParse-b { | ||
font-weight: 700 | ||
} | ||
|
||
.brax-strikethrough { | ||
text-decoration: line-through | ||
} | ||
|
||
.brax-redact { | ||
color: #000; | ||
background-color: #000; | ||
transition: .2s | ||
} | ||
|
||
.brax-redact:active,.brax-redact:hover { | ||
color: auto; | ||
background-color: transparent | ||
} | ||
|
||
.brax-italic,.braxParse-i { | ||
font-style: italic | ||
} | ||
|
||
.brax-underline,.braxParse-u { | ||
text-decoration: underline | ||
} | ||
|
||
.brax-underline-through { | ||
text-decoration: underline line-through | ||
} | ||
|
||
.dictJikipedia-Title { | ||
font-size: 1.2em; | ||
|
||
a { | ||
color: currentColor; | ||
} | ||
} | ||
|
||
.dictJikipedia-Item { | ||
margin-bottom: 10px; | ||
} | ||
|
||
.dictJikipedia-Content { | ||
margin: 0 0 3px 0; | ||
} | ||
|
||
.dictJikipedia-Image { | ||
text-align: center; | ||
|
||
> img { | ||
max-width: 80%; | ||
max-height: 200px; | ||
} | ||
} | ||
|
||
.dictJikipedia-Footer { | ||
color: #666; | ||
} | ||
|
||
.dictJikipedia-Author { | ||
color: #5caf9e; | ||
margin-right: 1em; | ||
} | ||
|
||
.dictJikipedia-Thumbs { | ||
margin-right: 5px; | ||
} | ||
|
||
.dictJikipedia-IconThumbsUp { | ||
width: 0.9em; | ||
height: 0.9em; | ||
fill: #666; | ||
margin-right: 2px; | ||
} |
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,39 @@ | ||
import { DictItem } from '@/app-config/dicts' | ||
|
||
export type UrbanConfig = DictItem<{ | ||
resultnum: number | ||
}> | ||
|
||
export default (): UrbanConfig => ({ | ||
lang: '01000000', | ||
selectionLang: { | ||
english: true, | ||
chinese: true, | ||
japanese: false, | ||
korean: false, | ||
french: false, | ||
spanish: false, | ||
deutsch: false, | ||
others: false, | ||
matchAll: false | ||
}, | ||
defaultUnfold: { | ||
english: true, | ||
chinese: true, | ||
japanese: true, | ||
korean: true, | ||
french: true, | ||
spanish: true, | ||
deutsch: true, | ||
others: true, | ||
matchAll: false | ||
}, | ||
preferredHeight: 380, | ||
selectionWC: { | ||
min: 1, | ||
max: 5 | ||
}, | ||
options: { | ||
resultnum: 4 | ||
} | ||
}) |
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,95 @@ | ||
import { fetchDirtyDOM } from '@/_helpers/fetch-dom' | ||
import { | ||
HTMLString, | ||
getText, | ||
getInnerHTML, | ||
handleNoResult, | ||
handleNetWorkError, | ||
SearchFunction, | ||
GetSrcPageFunction, | ||
DictSearchResult, | ||
getFullLink | ||
} from '../helpers' | ||
|
||
export const getSrcPage: GetSrcPageFunction = text => { | ||
return `https://jikipedia.com/search?phrase=${encodeURIComponent(text)}` | ||
} | ||
|
||
const HOST = 'https://jikipedia.com' | ||
|
||
interface JikipediaResultItem { | ||
title: string | ||
content: HTMLString | ||
likes: number | ||
url?: string | ||
author?: { | ||
name: string | ||
url: string | ||
} | ||
} | ||
|
||
export type JikipediaResult = JikipediaResultItem[] | ||
|
||
type JikipediaSearchResult = DictSearchResult<JikipediaResult> | ||
|
||
export const search: SearchFunction<JikipediaResult> = ( | ||
text, | ||
config, | ||
profile, | ||
payload | ||
) => { | ||
const options = profile.dicts.all.jikipedia.options | ||
|
||
return fetchDirtyDOM( | ||
`https://jikipedia.com/search?phrase=${encodeURIComponent(text)}` | ||
) | ||
.catch(handleNetWorkError) | ||
.then(doc => handleDOM(doc, options)) | ||
} | ||
|
||
function handleDOM( | ||
doc: Document, | ||
{ resultnum }: { resultnum: number } | ||
): JikipediaSearchResult | Promise<JikipediaSearchResult> { | ||
const $cards = doc.querySelectorAll('.lite-card') | ||
if ($cards.length < 1) { | ||
return handleNoResult() | ||
} | ||
|
||
doc.querySelectorAll('.ad-card').forEach(el => el.remove()) | ||
|
||
const result: JikipediaResult = [] | ||
|
||
for (const $card of $cards) { | ||
if (result.length >= resultnum) { | ||
break | ||
} | ||
|
||
const item: JikipediaResultItem = { | ||
title: getText($card, '.title'), | ||
content: getInnerHTML(HOST, $card, '.content'), | ||
likes: Number(getText($card, '.like-count')) || 0 | ||
} | ||
|
||
if (!item.content) { | ||
continue | ||
} | ||
|
||
const $a = $card.querySelector('a.card-content') | ||
if ($a) { | ||
item.url = getFullLink(HOST, $a, 'href') | ||
} | ||
|
||
const $author = $card.querySelector('.author a') | ||
if ($author) { | ||
item.author = { | ||
name: getText($author), | ||
url: getFullLink(HOST, $author, 'href') | ||
} | ||
} | ||
|
||
result.push(item) | ||
} | ||
|
||
return result.length > 0 ? { result } : handleNoResult() | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions
18
test/specs/components/dictionaries/jikipedia/engine.spec.ts
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,18 @@ | ||
import { retry } from '../helpers' | ||
import { search } from '@/components/dictionaries/jikipedia/engine' | ||
import { getDefaultConfig } from '@/app-config' | ||
import { getDefaultProfile } from '@/app-config/profiles' | ||
|
||
describe('Dict/Jikipedia/engine', () => { | ||
it('should parse result correctly', () => { | ||
return retry(() => | ||
search('xswl', getDefaultConfig(), getDefaultProfile(), { | ||
isPDF: false | ||
}).then(searchResult => { | ||
expect(typeof searchResult.result.length).toBeGreaterThan(0) | ||
expect(searchResult.result[0].title).toBe('string') | ||
expect(searchResult.result[0].content).toBe('string') | ||
}) | ||
) | ||
}) | ||
}) |
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,3 @@ | ||
module.exports = { | ||
files: [['xswl.html', 'https://jikipedia.com/search?phrase=xswl']] | ||
} |
9 changes: 9 additions & 0 deletions
9
test/specs/components/dictionaries/jikipedia/requests.mock.ts
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,9 @@ | ||
import { MockRequest } from '@/components/dictionaries/helpers' | ||
|
||
export const mockSearchTexts = ['xswl'] | ||
|
||
export const mockRequest: MockRequest = mock => { | ||
mock | ||
.onGet(/jikipedia/) | ||
.reply(200, require(`raw-loader!./response/xswl.html`).default) | ||
} |