├── .babelrc ├── .editorconfig ├── .gitattributes ├── .gitignore ├── .vscode └── setting.json ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── postcss.config.js ├── prettier.config.js ├── project.config.json ├── src ├── api │ └── index.js ├── app.mina ├── assets │ ├── github-r.png │ └── opensans.css ├── components │ ├── comment.mina │ ├── item.mina │ ├── reader.mina │ └── spinner.mina ├── pages │ ├── article.mina │ ├── item.mina │ ├── list.mina │ └── user.mina ├── store │ ├── index.js │ ├── modules │ │ ├── config.js │ │ ├── items.js │ │ ├── lists.js │ │ └── users.js │ └── types.js └── utils │ └── filters.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["@babel/preset-env", { 4 | "modules": false, 5 | "loose": true 6 | }] 7 | ], 8 | "plugins": [ 9 | ["@babel/plugin-transform-runtime", { 10 | "corejs": 2 11 | }], 12 | "@babel/plugin-proposal-class-properties", 13 | "@babel/plugin-proposal-export-default-from" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # GitLab 2 | # https://docs.gitlab.com/ee/user/project/highlighting.html 3 | *.wxml gitlab-language=xml 4 | *.wxss gitlab-language=css 5 | *.wxs gitlab-language=js 6 | *.mina gitlab-language=html 7 | 8 | # GitHub 9 | # https://github.com/github/linguist#overrides 10 | *.wxml linguist-language=xml 11 | *.wxss linguist-language=css 12 | *.wxs linguist-language=js 13 | *.mina linguist-language=html 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # dev 2 | .DS_Store 3 | node_modules 4 | npm-debug.log 5 | lerna-debug.log 6 | dist 7 | -------------------------------------------------------------------------------- /.vscode/setting.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "*.wxml": "xml", 4 | "*.wxss": "css", 5 | "*.wxs": "js", 6 | "*.mina": "vue" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tina-hackernews 2 | > A [Tina](https://github.com/tinajs/tina) powered **Wechat-Mini-Program** implementation of [Hacker News](https://news.ycombinator.com/), heavily inspired by [Vue-Hackernews-2.0](https://github.com/vuejs/vue-hackernews-2.0) but runs as a Wechat-Mini-Program. 3 | 4 | [![license](https://img.shields.io/github/license/tinajs/tina-hackernews.svg?style=flat-square)](./LICENSE) 5 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) 6 | 7 | ## Live Version 8 | 9 | 10 | *Scan this wxcode on [Wechat](http://weixin.qq.com/).* 11 | 12 | ## Building 13 | **Requires Node.js LTS** 14 | 15 | Install dependencies 16 | ```bash 17 | npm i 18 | ``` 19 | 20 | ### npm script 21 | - ``npm start`` - start in development mode 22 | - ``npm build`` - build for production 23 | 24 | ## Related Projects 25 | - [Tina.js](https://github.com/tinajs/tina) 26 | - [mina-webpack](https://github.com/tinajs/mina-webpack) 27 | - [template-mina](https://github.com/tinajs/template-mina) 28 | - [Tinax](https://github.com/tinajs/tinax) 29 | - [tina-router](https://github.com/tinajs/tina-router) 30 | - [tina-loading](https://github.com/tinajs/tina-loading) 31 | - [tina-modal](https://github.com/tinajs/tina-modal) 32 | - [Vue-Hackernews-2.0](https://github.com/vuejs/vue-hackernews-2.0) 33 | 34 | ## License 35 | Apache-2.0 © [yelo](https://github.com/imyelo), 2017 - present 36 | 37 | [![](https://github.com/tinajs/assets/raw/master/images/banners/sponsored.png)](https://github.com/tinajs/tina) 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@tinajs/tina-hackernews", 3 | "private": true, 4 | "version": "0.1.0", 5 | "description": "", 6 | "author": "yelo", 7 | "license": "MIT", 8 | "scripts": { 9 | "clean": "rimraf \"./dist/!(app.json)**\"", 10 | "prestart": "run-s clean", 11 | "prebuild": "run-s clean", 12 | "start": "webpack -w", 13 | "build": "cross-env NODE_ENV=production webpack" 14 | }, 15 | "dependencies": { 16 | "@tinajs/tina": "^1.5.0", 17 | "@tinajs/tina-loading": "^0.2.0", 18 | "@tinajs/tina-modal": "^0.2.0", 19 | "@tinajs/tina-router": "^0.5.1", 20 | "@tinajs/tinax": "^0.1.2", 21 | "compose-function": "^3.0.3", 22 | "date-fns": "^1.29.0", 23 | "he": "^1.1.1", 24 | "just-map-object": "^1.1.24", 25 | "keymirror": "^0.1.1", 26 | "p-queue": "^2.3.0", 27 | "p-timeout": "^2.0.1", 28 | "posthtml": "^0.10.1", 29 | "promise.prototype.finally": "^3.1.0", 30 | "uniq": "^1.0.1", 31 | "wxio": "^0.3.3" 32 | }, 33 | "devDependencies": { 34 | "@babel/core": "^7.1.0", 35 | "@babel/plugin-proposal-class-properties": "^7.1.0", 36 | "@babel/plugin-proposal-export-default-from": "^7.0.0", 37 | "@babel/plugin-transform-runtime": "^7.1.0", 38 | "@babel/preset-env": "^7.1.0", 39 | "@babel/runtime-corejs2": "^7.0.0", 40 | "@tinajs/mina-entry-webpack-plugin": "^1.0.8", 41 | "@tinajs/mina-loader": "^1.2.0", 42 | "@tinajs/mina-runtime-webpack-plugin": "^1.0.2", 43 | "babel-loader": "^8.0.4", 44 | "cross-env": "^5.1.1", 45 | "file-loader": "^1.1.11", 46 | "npm-run-all": "^4.1.2", 47 | "postcss-color-function": "^4.0.1", 48 | "postcss-loader": "^2.0.8", 49 | "precss": "^2.0.0", 50 | "rimraf": "^2.6.2", 51 | "uglify-loader": "^2.0.0", 52 | "uglifyjs-webpack-plugin": "^1.1.2", 53 | "url-loader": "^0.6.2", 54 | "webpack": "^4.23.1", 55 | "webpack-cli": "^3.1.0" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: [ 3 | require('precss')(), 4 | require('postcss-color-function')(), 5 | ], 6 | } 7 | -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | // @see https://prettier.io/docs/en/options.html 2 | module.exports = { 3 | semi: false, 4 | singleQuote: true, 5 | trailingComma: 'es5', 6 | } 7 | -------------------------------------------------------------------------------- /project.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "miniprogramRoot": "dist/", 3 | "compileType": "miniprogram", 4 | "setting": { 5 | "urlCheck": false, 6 | "es6": false, 7 | "postcss": false, 8 | "minified": false, 9 | "newFeature": true, 10 | "nodeModules": false, 11 | "autoAudits": false, 12 | "uglifyFileName": false 13 | }, 14 | "appid": "wx7b964a592d5fd8e9", 15 | "projectname": "HackerNews热点", 16 | "condition": {} 17 | } 18 | -------------------------------------------------------------------------------- /src/api/index.js: -------------------------------------------------------------------------------- 1 | import wxio from 'wxio' 2 | 3 | const isDevtools = wx.getSystemInfoSync().platform === 'devtools' 4 | 5 | const HACKERNEWS_API_BASE_URL = isDevtools ? `https://cors-anywhere.herokuapp.com/https://hacker-news.firebaseio.com/v0/` : 'https://tina-hackernews.lab4310.com/hn/v0/' 6 | const ARTICLE_API_BASE_URL = 'https://tina-hackernews.lab4310.com/readability/' 7 | const CONFIG_API_URL = 'https://tina-hackernews.lab4310.com/config' 8 | 9 | function got (url) { 10 | return wxio.request({ url }) 11 | .then(function (response) { 12 | return response.data || {} 13 | }) 14 | } 15 | 16 | function fetch (child) { 17 | return got(`${HACKERNEWS_API_BASE_URL}${child}.json`) 18 | } 19 | 20 | export function fetchIdsByChannel (channel) { 21 | return fetch(`${channel}stories`) 22 | } 23 | 24 | export function fetchItem (id) { 25 | return fetch(`item/${id}`) 26 | .then((item) => { 27 | if (!item) { 28 | return item 29 | } 30 | return { 31 | ...item, 32 | __lastUpdated: Date.now(), 33 | } 34 | }) 35 | } 36 | 37 | export function fetchItems (ids) { 38 | return Promise.all(ids.map((id) => fetchItem(id))) 39 | } 40 | 41 | export function fetchUser (id) { 42 | return fetch(`user/${id}`) 43 | } 44 | 45 | export function fetchArticle (id) { 46 | return got(`${ARTICLE_API_BASE_URL}${id}`) 47 | } 48 | 49 | export function fetchConfig () { 50 | return got(CONFIG_API_URL) 51 | } 52 | -------------------------------------------------------------------------------- /src/app.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "window": { 4 | "navigationBarBackgroundColor": "#ff6600", 5 | "navigationBarTextStyle": "white", 6 | "navigationBarTitleText": " Hacker News ", 7 | "backgroundColor": "#ff6600", 8 | "backgroundTextStyle": "light" 9 | }, 10 | "pages": [ 11 | "pages/list.mina", 12 | "pages/user.mina", 13 | "pages/item.mina", 14 | "pages/article.mina" 15 | ] 16 | } 17 | 18 | 19 | 35 | 36 | 48 | -------------------------------------------------------------------------------- /src/assets/github-r.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tinajs/tina-hackernews/420f0771455394602ce3d6f93df106b06af82672/src/assets/github-r.png -------------------------------------------------------------------------------- /src/assets/opensans.css: -------------------------------------------------------------------------------- 1 | /* cyrillic-ext */ 2 | @font-face { 3 | font-family: 'Open Sans'; 4 | font-style: normal; 5 | font-weight: 400; 6 | src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://gstatic.cat.net/s/opensans/v14/K88pR3goAWT7BTt32Z01mxJtnKITppOI_IvcXXDNrsc.woff2) format('woff2'); 7 | unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F; 8 | } 9 | /* cyrillic */ 10 | @font-face { 11 | font-family: 'Open Sans'; 12 | font-style: normal; 13 | font-weight: 400; 14 | src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://gstatic.cat.net/s/opensans/v14/RjgO7rYTmqiVp7vzi-Q5URJtnKITppOI_IvcXXDNrsc.woff2) format('woff2'); 15 | unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; 16 | } 17 | /* greek-ext */ 18 | @font-face { 19 | font-family: 'Open Sans'; 20 | font-style: normal; 21 | font-weight: 400; 22 | src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://gstatic.cat.net/s/opensans/v14/LWCjsQkB6EMdfHrEVqA1KRJtnKITppOI_IvcXXDNrsc.woff2) format('woff2'); 23 | unicode-range: U+1F00-1FFF; 24 | } 25 | /* greek */ 26 | @font-face { 27 | font-family: 'Open Sans'; 28 | font-style: normal; 29 | font-weight: 400; 30 | src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://gstatic.cat.net/s/opensans/v14/xozscpT2726on7jbcb_pAhJtnKITppOI_IvcXXDNrsc.woff2) format('woff2'); 31 | unicode-range: U+0370-03FF; 32 | } 33 | /* vietnamese */ 34 | @font-face { 35 | font-family: 'Open Sans'; 36 | font-style: normal; 37 | font-weight: 400; 38 | src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://gstatic.cat.net/s/opensans/v14/59ZRklaO5bWGqF5A9baEERJtnKITppOI_IvcXXDNrsc.woff2) format('woff2'); 39 | unicode-range: U+0102-0103, U+1EA0-1EF9, U+20AB; 40 | } 41 | /* latin-ext */ 42 | @font-face { 43 | font-family: 'Open Sans'; 44 | font-style: normal; 45 | font-weight: 400; 46 | src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://gstatic.cat.net/s/opensans/v14/u-WUoqrET9fUeobQW7jkRRJtnKITppOI_IvcXXDNrsc.woff2) format('woff2'); 47 | unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF; 48 | } 49 | /* latin */ 50 | @font-face { 51 | font-family: 'Open Sans'; 52 | font-style: normal; 53 | font-weight: 400; 54 | src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://gstatic.cat.net/s/opensans/v14/cJZKeOuBrn4kERxqtaUH3VtXRa8TVwTICgirnJhmVJw.woff2) format('woff2'); 55 | unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215; 56 | } 57 | -------------------------------------------------------------------------------- /src/components/comment.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "usingComponents": { 4 | "hn-comment": "./comment.mina", 5 | "spinner": "./spinner.mina" 6 | }, 7 | "component": true 8 | } 9 | 10 | 11 | 35 | 36 | 88 | 89 | 144 | -------------------------------------------------------------------------------- /src/components/item.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "component": true, 4 | "usingComponents": { 5 | "spinner": "./spinner.mina" 6 | } 7 | } 8 | 9 | 10 | 33 | 34 | 57 | 58 | 109 | -------------------------------------------------------------------------------- /src/components/reader.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "component": true, 4 | "usingComponents": { 5 | "spinner": "./spinner.mina" 6 | } 7 | } 8 | 9 | 10 | 28 | 29 | 95 | 96 | 181 | -------------------------------------------------------------------------------- /src/components/spinner.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "component": true 4 | } 5 | 6 | 7 | 10 | 11 | 19 | 20 | 44 | -------------------------------------------------------------------------------- /src/pages/article.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "navigationBarBackgroundColor": "#ffffff", 4 | "navigationBarTextStyle": "black", 5 | "backgroundColor": "#f2f3f5", 6 | "backgroundTextStyle": "dark", 7 | "usingComponents": { 8 | "hn-reader": "/components/reader.mina" 9 | } 10 | } 11 | 12 | 13 | 21 | 22 | 54 | 55 | 82 | -------------------------------------------------------------------------------- /src/pages/item.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "usingComponents": { 4 | "hn-comment": "/components/comment.mina", 5 | "spinner": "/components/spinner.mina" 6 | } 7 | } 8 | 9 | 10 | 34 | 35 | 133 | 134 | 185 | -------------------------------------------------------------------------------- /src/pages/list.mina: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "enablePullDownRefresh": true, 4 | "onReachBottomDistance": 192, 5 | "usingComponents": { 6 | "hn-item": "/components/item.mina", 7 | "spinner": "/components/spinner.mina" 8 | } 9 | } 10 | 11 | 12 | 36 | 37 | 164 | 165 | 238 | -------------------------------------------------------------------------------- /src/pages/user.mina: -------------------------------------------------------------------------------- 1 | 19 | 20 | 65 | 66 | 99 | -------------------------------------------------------------------------------- /src/store/index.js: -------------------------------------------------------------------------------- 1 | import Tinax from '@tinajs/tinax' 2 | 3 | import config from './modules/config' 4 | import items from './modules/items' 5 | import lists from './modules/lists' 6 | import users from './modules/users' 7 | 8 | export const tinax = new Tinax({ 9 | modules: { 10 | config, 11 | items, 12 | lists, 13 | users, 14 | }, 15 | }) 16 | 17 | // for debug 18 | global.tinax = tinax 19 | -------------------------------------------------------------------------------- /src/store/modules/config.js: -------------------------------------------------------------------------------- 1 | import uniq from 'uniq' 2 | import map from 'just-map-object' 3 | import types from '../types' 4 | import { 5 | fetchConfig, 6 | } from '../../api' 7 | 8 | const initialState = { 9 | github: false, 10 | ads: { 11 | menu: { 12 | visible: true, 13 | thumbnail: '', 14 | message: { 15 | type: 'link', 16 | link: { 17 | title: 'HackerNews 热点 @ GitHub', 18 | url: 'https://github.com/tinajs/tina-hackernews', 19 | thumbnail: 'https://github.com/tinajs/assets/raw/master/images/showcases/hackernews.png', 20 | }, 21 | }, 22 | }, 23 | } 24 | } 25 | 26 | const getters = { 27 | ads: (state) => { 28 | return map(state.ads, (key, ad) => { 29 | return { 30 | ...ad, 31 | message: JSON.stringify(ad.message), 32 | } 33 | }) 34 | }, 35 | } 36 | 37 | const actions = { 38 | fetchConfig ({ commit, state }) { 39 | fetchConfig().then((config) => commit(types.SET_CONFIG, { config })) 40 | }, 41 | } 42 | 43 | const mutations = { 44 | [types.SET_CONFIG] (state, { config }) { 45 | return config 46 | }, 47 | } 48 | 49 | export default { 50 | state: initialState, 51 | getters, 52 | actions, 53 | mutations, 54 | } 55 | -------------------------------------------------------------------------------- /src/store/modules/items.js: -------------------------------------------------------------------------------- 1 | import uniq from 'uniq' 2 | import types from '../types' 3 | import { 4 | fetchItems, 5 | fetchItem, 6 | } from '../../api' 7 | 8 | const CACHE_EXPIRES_IN = 1000 * 60 * 3 9 | 10 | const initialState = [] 11 | 12 | const getters = { 13 | activeItems: (state, getters) => { 14 | return getters.activeIds().map(id => state.find((item) => item.id === id) || { id, loading: true }) 15 | }, 16 | getItem: (state) => (id) => state.find((item) => item.id === id) || { id, loading: true }, 17 | } 18 | 19 | const actions = { 20 | ensureActiveItems ({ dispatch, getters }) { 21 | return dispatch('fetchItems', { 22 | ids: getters.activeIds(), 23 | }) 24 | }, 25 | fetchItems ({ commit, state }, { ids }) { 26 | const now = Date.now() 27 | ids = ids.filter(id => { 28 | const item = state.find((item) => item.id === id) 29 | if (!item) { 30 | return true 31 | } 32 | if (now - item.__lastUpdated > CACHE_EXPIRES_IN) { 33 | return true 34 | } 35 | return false 36 | }) 37 | if (ids.length) { 38 | return Promise.all(ids.map((id) => fetchItem(id).then((item) => commit(types.SET_ITEM, { item })))) 39 | } else { 40 | return Promise.resolve() 41 | } 42 | }, 43 | } 44 | 45 | const mutations = { 46 | [types.SET_ITEM] (state, { item }) { 47 | return uniq([ ...state, item ], (l, r) => l.id === r.id ? 0 : -1) 48 | }, 49 | } 50 | 51 | export default { 52 | state: initialState, 53 | getters, 54 | actions, 55 | mutations, 56 | } 57 | -------------------------------------------------------------------------------- /src/store/modules/lists.js: -------------------------------------------------------------------------------- 1 | import types from '../types' 2 | import { 3 | fetchIdsByChannel, 4 | } from '../../api' 5 | 6 | const MINIMUM_PAGE = 1 7 | const PAGE_SIZE = 10 8 | 9 | const DEFAULT_CHANNEL = 'top' 10 | const DEFAULT_PAGE = MINIMUM_PAGE 11 | 12 | const initialState = { 13 | activeChannel: DEFAULT_CHANNEL, 14 | activePage: DEFAULT_PAGE, 15 | top: [], 16 | new: [], 17 | show: [], 18 | ask: [], 19 | job: [], 20 | } 21 | 22 | const getters = { 23 | activeIds: (state) => state[state.activeChannel].slice(0, state.activePage * PAGE_SIZE), 24 | maxPageOfActiveChannel: (state) => Math.ceil(state[state.activeChannel].length / PAGE_SIZE), 25 | hasMoreItemsOfActiveChannel: (state, getters) => state.activePage < getters.maxPageOfActiveChannel(), 26 | } 27 | 28 | const actions = { 29 | fetchListData ({ commit, dispatch }, { channel }) { 30 | commit(types.SET_ACTIVE_CHANNEL, { channel }) 31 | return fetchIdsByChannel(channel) 32 | .then((ids) => commit(types.SET_LIST, { channel, ids })) 33 | .then(() => dispatch('ensureActiveItems')) 34 | }, 35 | fetchNextPage ({ commit, state, dispatch, getters }) { 36 | commit(types.SET_ACTIVE_PAGE, { page: Math.min(state.activePage + 1, getters.maxPageOfActiveChannel()) }) 37 | return dispatch('ensureActiveItems') 38 | }, 39 | } 40 | 41 | const mutations = { 42 | [types.SET_ACTIVE_CHANNEL] (state, { channel }) { 43 | return { 44 | ...state, 45 | activeChannel: channel, 46 | activePage: DEFAULT_PAGE, 47 | } 48 | }, 49 | [types.SET_LIST] (state, { channel, ids }) { 50 | return { 51 | ...state, 52 | [channel]: ids, 53 | } 54 | }, 55 | [types.SET_ACTIVE_PAGE] (state, { page }) { 56 | return { 57 | ...state, 58 | activePage: page, 59 | } 60 | }, 61 | } 62 | 63 | export default { 64 | state: initialState, 65 | getters, 66 | actions, 67 | mutations, 68 | } 69 | -------------------------------------------------------------------------------- /src/store/modules/users.js: -------------------------------------------------------------------------------- 1 | import uniq from 'uniq' 2 | import types from '../types' 3 | import { 4 | fetchUser, 5 | } from '../../api' 6 | 7 | const CACHE_EXPIRES_IN = 1000 * 60 * 3 8 | 9 | const initialState = [] 10 | 11 | const getters = { 12 | } 13 | 14 | const actions = { 15 | fetchUser ({ commit, state }, { id }) { 16 | const found = state.find((user) => user.id === id) 17 | return found 18 | ? Promise.resolve(found) 19 | : fetchUser(id).then((user) => commit(types.SET_USER, { id, user })) 20 | }, 21 | } 22 | 23 | const mutations = { 24 | [types.SET_USER] (state, { id, user }) { 25 | return uniq([ ...state, user ], (l, r) => l.id === r.id ? 0 : -1) 26 | }, 27 | } 28 | 29 | export default { 30 | state: initialState, 31 | getters, 32 | actions, 33 | mutations, 34 | } 35 | -------------------------------------------------------------------------------- /src/store/types.js: -------------------------------------------------------------------------------- 1 | import km from 'keymirror' 2 | 3 | export default km({ 4 | SET_CONFIG: null, 5 | SET_ACTIVE_CHANNEL: null, 6 | SET_ACTIVE_PAGE: null, 7 | SET_LIST: null, 8 | SET_ITEMS: null, 9 | SET_ITEM: null, 10 | SET_USER: null, 11 | }) 12 | -------------------------------------------------------------------------------- /src/utils/filters.js: -------------------------------------------------------------------------------- 1 | export function host (url) { 2 | const host = url.replace(/^https?:\/\//, '').replace(/\/.*$/, '') 3 | const parts = host.split('.').slice(-3) 4 | if (parts[0] === 'www') parts.shift() 5 | return parts.join('.') 6 | } 7 | 8 | export function timeAgo (time) { 9 | const between = Date.now() / 1000 - Number(time) 10 | if (between < 3600) { 11 | return pluralize(~~(between / 60), ' minute') 12 | } else if (between < 86400) { 13 | return pluralize(~~(between / 3600), ' hour') 14 | } else { 15 | return pluralize(~~(between / 86400), ' day') 16 | } 17 | } 18 | 19 | export function pluralize (time, label) { 20 | if (time === 1) { 21 | return time + label 22 | } 23 | return time + label + 's' 24 | } 25 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | const webpack = require('webpack') 3 | const MinaEntryPlugin = require('@tinajs/mina-entry-webpack-plugin') 4 | const MinaRuntimePlugin = require('@tinajs/mina-runtime-webpack-plugin') 5 | 6 | const isProduction = process.env.NODE_ENV === 'production' 7 | 8 | const loaders = { 9 | script: 'babel-loader', 10 | style: { 11 | loader: 'postcss-loader', 12 | options: { 13 | config: { 14 | path: resolve('./postcss.config.js'), 15 | }, 16 | }, 17 | }, 18 | } 19 | 20 | module.exports = { 21 | context: resolve('src'), 22 | entry: './app.mina', 23 | output: { 24 | path: resolve('dist'), 25 | filename: '[name]', 26 | publicPath: '/', 27 | globalObject: 'wx', 28 | }, 29 | module: { 30 | rules: [ 31 | { 32 | test: /\.mina$/, 33 | exclude: /node_modules/, 34 | use: [ 35 | { 36 | loader: '@tinajs/mina-loader', 37 | options: { 38 | loaders, 39 | }, 40 | }, 41 | ], 42 | }, 43 | { 44 | test: /\.mina$/, 45 | include: /node_modules/, 46 | use: '@tinajs/mina-loader', 47 | }, 48 | { 49 | test: /\.js$/, 50 | include: [ 51 | resolve('src'), 52 | resolve('node_modules/p-timeout'), 53 | resolve('node_modules/p-queue'), 54 | ], 55 | use: loaders.script, 56 | }, 57 | { 58 | test: /\.(css|wxss)$/, 59 | exclude: /node_modules/, 60 | use: loaders.style, 61 | }, 62 | { 63 | test: /\.(png|jpg|jpeg|gif|svg)$/, 64 | use: { 65 | loader: 'file-loader', 66 | options: { 67 | name: 'assets/[name].[hash:6].[ext]', 68 | }, 69 | }, 70 | }, 71 | ], 72 | }, 73 | resolve: { 74 | symlinks: true, 75 | }, 76 | plugins: [ 77 | new webpack.EnvironmentPlugin({ 78 | NODE_ENV: 'development', 79 | DEBUG: false, 80 | }), 81 | new MinaEntryPlugin(), 82 | new MinaRuntimePlugin(), 83 | ], 84 | optimization: { 85 | splitChunks: { 86 | chunks: 'all', 87 | name: 'common.js', 88 | minChunks: 2, 89 | minSize: 0, 90 | }, 91 | runtimeChunk: { 92 | name: 'runtime.js', 93 | }, 94 | }, 95 | mode: isProduction ? 'production' : 'none', 96 | } 97 | --------------------------------------------------------------------------------