11const express = require ( "express" ) ;
2- const mf2 = require ( "microformat-node " ) ;
2+ const { mf2 } = require ( "microformats-parser " ) ;
33const undici = require ( "undici" ) ;
4- const querystring = require ( "querystring" ) ;
54const pkg = require ( "./package.json" ) ;
65const app = express ( ) ;
76const port = process . env . PORT || 9000 ;
87
8+ function getDependencyVersion ( dependencyName ) {
9+ const fs = require ( 'fs' ) ;
10+ const lockfile = require ( '@yarnpkg/lockfile' ) ;
11+ const parsed = lockfile . parse ( fs . readFileSync ( "./yarn.lock" , "utf-8" ) ) ;
12+ if ( parsed . type !== "success" ) return "unknown" ;
13+ const dependency = parsed . object [ `${ dependencyName } @${ pkg . dependencies [ dependencyName ] } ` ] ;
14+ if ( dependency === undefined ) return "unknown" ;
15+ return dependency . version ;
16+ }
17+ const mf2version = getDependencyVersion ( "microformats-parser" ) ;
18+
919function htmlToMf2 ( url , html , res ) {
10- mf2 . get ( { baseUrl : url , html } , ( err , data ) => {
11- const body = err || data ;
12- res
13- . header ( "content-type" , "application/json; charset=UTF-8" )
14- . send ( JSON . stringify ( body , null , 2 ) ) ;
15- } ) ;
20+ const body = mf2 ( html , { baseUrl : url } ) ;
21+ res
22+ . header ( "content-type" , "application/json; charset=UTF-8" )
23+ . send ( JSON . stringify ( body , null , 2 ) ) ;
1624}
1725
1826app . set ( "view engine" , "ejs" ) ;
@@ -31,13 +39,12 @@ app.get("/", async (req, res) => {
3139 htmlToMf2 ( url , text , res ) ;
3240 } else {
3341 res . render ( "index.html.ejs" , {
34- version : `${ pkg . version } (lib: ${ mf2 . version } )` ,
42+ version : `${ pkg . version } (lib: ${ mf2version } )` ,
3543 } ) ;
3644 }
3745} ) ;
38- app . post ( "/" , ( req , res ) => {
39- const qsBody = querystring . parse ( req . body ) ;
40- htmlToMf2 ( qsBody . url , qsBody . html , res ) ;
46+ app . post ( "/" , express . urlencoded ( { extended : false } ) , ( req , res ) => {
47+ htmlToMf2 ( req . body . url , req . body . html , res ) ;
4148} ) ;
4249
4350app . listen ( port , ( ) => {
0 commit comments