-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement MediaWiki REST API render (partial impl)
- Loading branch information
1 parent
af4f200
commit 8c7ba86
Showing
16 changed files
with
171 additions
and
67 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
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
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,62 @@ | ||
import * as logger from '../Logger.js' | ||
import { Renderer } from './abstract.renderer.js' | ||
import { getStrippedTitleFromHtml } from '../util/misc.js' | ||
import { RenderOpts, RenderOutput } from './abstract.renderer.js' | ||
|
||
/* | ||
Represent 'https://{wikimedia-wiki}/w/rest.php/v1/page/{title}/with_html' | ||
or | ||
'https://{3rd-part-wikimedia-wiki}/w/rest.php/v1/page/{title}/with_html' | ||
*/ | ||
export class MediawikiRESTApiRenderer extends Renderer { | ||
constructor() { | ||
super() | ||
} | ||
|
||
private async retrieveHtml(renderOpts: RenderOpts): Promise<any> { | ||
const { data, articleId, articleDetail, isMainPage } = renderOpts | ||
|
||
if (!data) { | ||
throw new Error(`Cannot render [${data}] into an article`) | ||
} | ||
|
||
let html: string | ||
let displayTitle: string | ||
let strippedTitle: string | ||
|
||
if (data.html) { | ||
html = isMainPage ? data.html : super.injectH1TitleToHtml(data.html, articleDetail) | ||
strippedTitle = getStrippedTitleFromHtml(html) | ||
displayTitle = strippedTitle || articleId.replace('_', ' ') | ||
return { html, displayTitle } | ||
} else if (data.errorKey) { | ||
logger.error(`Error in retrieved article [${articleId}]:`, data.errorKey) | ||
return '' | ||
} | ||
logger.error('Unable to parse data from visual editor') | ||
return '' | ||
} | ||
|
||
public async render(renderOpts: RenderOpts): Promise<any> { | ||
try { | ||
const result: RenderOutput = [] | ||
const { articleId, articleDetail, webp, _moduleDependencies, dump } = renderOpts | ||
const { html, displayTitle } = await this.retrieveHtml(renderOpts) | ||
if (html) { | ||
const { finalHTML, mediaDependencies, subtitles } = await super.processHtml(html, dump, articleId, articleDetail, _moduleDependencies, webp) | ||
result.push({ | ||
articleId, | ||
displayTitle, | ||
html: finalHTML, | ||
mediaDependencies, | ||
subtitles, | ||
}) | ||
return result | ||
} | ||
return '' | ||
} catch (err) { | ||
logger.error(err.message) | ||
throw new Error(err.message) | ||
} | ||
} | ||
} |
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
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,17 @@ | ||
import urlBuilder from './url.builder.js' | ||
|
||
/** | ||
* Interface to build URLs based on MediaWiki REST API URL | ||
*/ | ||
export default class MediaWikiRESTApiURL { | ||
baseDomain: string | ||
|
||
constructor(baseDomain: string) { | ||
this.baseDomain = baseDomain | ||
} | ||
|
||
buildArticleURL(articleId: string) { | ||
const base = urlBuilder.setDomain(this.baseDomain).build() | ||
return `${base}${articleId}/with_html` | ||
} | ||
} |
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
Oops, something went wrong.