Skip to content

Commit

Permalink
feat(dicts): add jikipedia
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Apr 30, 2020
1 parent 61a883a commit 046b850
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app-config/dicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import google from '@/components/dictionaries/google/config'
import googledict from '@/components/dictionaries/googledict/config'
import guoyu from '@/components/dictionaries/guoyu/config'
import hjdict from '@/components/dictionaries/hjdict/config'
import jikipedia from '@/components/dictionaries/jikipedia/config'
import jukuu from '@/components/dictionaries/jukuu/config'
import lexico from '@/components/dictionaries/lexico/config'
import liangan from '@/components/dictionaries/liangan/config'
Expand Down Expand Up @@ -48,6 +49,7 @@ export const defaultAllDicts = {
googledict: googledict(),
guoyu: guoyu(),
hjdict: hjdict(),
jikipedia: jikipedia(),
jukuu: jukuu(),
lexico: lexico(),
liangan: liangan(),
Expand Down
51 changes: 51 additions & 0 deletions src/components/dictionaries/jikipedia/View.tsx
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
19 changes: 19 additions & 0 deletions src/components/dictionaries/jikipedia/_locales.json
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": ""
}
}
}
83 changes: 83 additions & 0 deletions src/components/dictionaries/jikipedia/_style.shadow.scss
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;
}
39 changes: 39 additions & 0 deletions src/components/dictionaries/jikipedia/config.ts
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
}
})
95 changes: 95 additions & 0 deletions src/components/dictionaries/jikipedia/engine.ts
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()
}
Binary file added src/components/dictionaries/jikipedia/favicon.png
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 test/specs/components/dictionaries/jikipedia/engine.spec.ts
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')
})
)
})
})
3 changes: 3 additions & 0 deletions test/specs/components/dictionaries/jikipedia/fixtures.js
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 test/specs/components/dictionaries/jikipedia/requests.mock.ts
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)
}

0 comments on commit 046b850

Please sign in to comment.