wikimedia/wikipedia-preview
Composer 安装命令:
composer require wikimedia/wikipedia-preview
包简介
Shows Wikipedia article preview in a popup
关键字:
README 文档
README
Wikipedia Preview
Wikipedia Preview is a JavaScript component that allows you to provide context from Wikipedia about words or phrases on any website. It lets you show a popup card with a short summary from Wikipedia when a reader hovers over a link.
Tasks and issues are tracked on Phabricator.
| Desktop | Mobile | Dark mode | Fullscreen gallery |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
Storybook: https://wikimedia.github.io/wikipedia-preview/main/storybook
Compatibility
| Browsers | Versions |
|---|---|
| Chrome, Firefox, Opera, Edge | Current and previous version |
| Internet Explorer | 11+ |
| Safari | 5.1+ |
| iOS | 6.1+ |
| Android | 4.1+ |
Features
- Works with any link that has an article on Wikipedia
- Supports LTR and RTL languages
- Works for articles with or without a lead image
Getting Started
Integrating Wikipedia Preview to your site consists of a three-step process:
- Loading wikipedia-preview.js (as a standalone script, WordPress plugin, or an npm dependency)
- Invoking
wikipediaPreview.init( <options> ) - Annotating articles accordingly
You can read more about each step below. Once Wikipedia Preview is set up correctly, you should see the version information being logged in your JS console. You can also invoke wikipediaPreview.version() from your JS console to view version information at any time.
WordPress Plugin
A WordPress plugin providing a thin wrapper around Wikipedia Preview to simplify its integration and usage within WordPress is available on GitHub and WordPress.org.
Standalone Script
<script src="wikipedia-preview.umd.cjs"></script> <script type="text/javascript"> wikipediaPreview.init() </script>
You can serve the file yourself or include it from unpkg (The library version 1.9.0 and later cannot be loaded from Unpkg due to a MIME type mismatch issue, the current workaround solution is adding the ?module parameter to get the correct content-type).
NPM
$ npm install wikipedia-preview --save
const wikipediaPreview = require('wikipedia-preview') wikipediaPreview.init()
Options of the init function
The init function accepts the following options:
| Name | Type | Default | Description |
|---|---|---|---|
| root | string,Element,Element[] | document |
Where to look for elements that should have the popup selector. Can be a selector to locate the root, a DOM Element, or an array of Elements |
| selector | string | '[data-wikipedia-preview]' |
How nodes that should have the popup are identified |
| lang | string | 'en' |
Default Wikipedia language |
| popupContainer | string,Element | document.body |
Where to put the popup in the DOM. Can be a selector or a DOM element. |
| detectLinks | Boolean | false |
Allow Wikipedia hyperlinks to have the popup |
| events | Object | {} |
Custom event handlers: { onShow: <fn>, onWikiRead: <fn> } |
| debug | Boolean | false |
Shows the debug message when init() is called |
| prefersColorScheme | string | 'detect' |
Sets theme color. Allowed values are 'light', 'dark' and 'detect'. Setting it to 'light' or 'dark' will dictate theme color regardless of prefers-color-scheme; setting to 'detect' will render preview according to prefers-color-scheme. |
Example (custom selector)
<p class="content"> You can learn about <span class="wiki">Chat</span> and <span class="wiki">Chien</span> from Wikipedia. </p> <div class="popup-container"></div>
wikipediaPreview.init({ root: document.querySelector('.content'), selector: '.wiki', popupContainer: '.popup-container', lang: 'fr' });
Example (detect Wikipedia links)
<p class="content"> You can learn about <a href="https://en.wikipedia.org/wiki/Chat">Chat</a> and <a href="https://en.wikipedia.org/wiki/Chien">Chien</a> from Wikipedia. </p>
wikipediaPreview.init({ detectLinks: true });
Example (custom event handlers)
wikipediaPreview.init({ events: { onShow: function(title, lang, type) { // call custom instrumentation here }, onWikiRead: function(title, lang) { // call custom instrumentation here } } });
Attributes
data-wikipedia-preview
To indicate that a word or expression should bring up the article preview popup, mark it with the data-wikipedia-preview attribute (or anything else as long as you're using the selector option described above).
data-wp-title
When the article title is not the same as the node's textContent property, use the data-wp-title attribute to specify the article title.
Note that the article title can include a section id. For example: Europe#Classical_antiquity
data-wp-lang
To use a language different than the language specified in the options, use the data-wp-lang attribute.
.wmf-wp-with-preview
To use the default trigger link styling:
- Add the following link to the page header:
<link href="wikipedia-preview-link.css" rel="stylesheet">
- Add the class
wmf-wp-with-previewto the node
If you prefer to style them in a way that makes more sense for your context, simply don't include the wikipedia-preview-link.css link in the header and add your own class to the node. Both of these are valid ways:
[data-wikipedia-preview] { background-color: yellow; }
.my-own-css-style { background-color: yellow; }
CSS custom properties
If you wish to adjust the styling of the light/dark theme, you can override the following CSS custom properties to your liking as shown below, under the appropriate prefers-color-scheme query.
@media (prefers-color-scheme: dark) { .wikipediapreview { --wikipediapreview-primary-background-color: #202122; --wikipediapreview-secondary-background-color: #202122; --wikipediapreview-primary-color: #eaecf0; --wikipediapreview-filter-setting: invert(1); } }
Acknowledgements/Contributors
This is heavily inspired by jquery.wikilookup and Page Previews.
wikimedia/wikipedia-preview 适用场景与选型建议
wikimedia/wikipedia-preview 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 77, 最近一次更新时间为 2023 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wiki」 「card」 「popup」 「wikipedia」 「facts」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wikimedia/wikipedia-preview 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wikimedia/wikipedia-preview 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wikimedia/wikipedia-preview 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
An extension to Semantic MediaWiki allowing to build breadcrumb links from an attributive property filter
Adds the EDTF data type to Wikibase
Fork from ghosh/Micromodal. Tiny, dependency-free javascript library for creating accessible modal dialogs.
Settings Card for Laravel Nova.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 77
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-19



