Public
Edited
Sep 22, 2023
1 fork
1 star
Insert cell
Insert cell
wtf=require('wtf_wikipedia')
Insert cell
{
let article =`An '''apple''' is a round, edible [[fruit]] produced by an '''apple tree''' ('''''Malus domestica'''''). Apple [[fruit tree|trees]] are [[agriculture|cultivated]] worldwide and are the most widely grown species in the [[genus]] ''[[Malus]]''. The [[tree]] originated in [[Central Asia]], where its wild ancestor, ''[[Malus sieversii]]'', is still found. Apples have been grown for thousands of years in Asia and Europe and were introduced to North America by [[European colonization of the Americas|European colonists]]. Apples have [[Religion|religious]] and [[mythology|mythological]] significance in many cultures, including [[Norse mythology|Norse]], [[Greek mythology|Greek]], and [[Christianity in Europe|European Christian]] tradition.

Apples grown from seed tend to be very different from those of their parents, and the resultant fruit frequently lacks desired characteristics. Generally, apple [[cultivar]]s are propagated by clonal [[grafting]] onto [[rootstock]]s. Apple trees grown without rootstocks tend to be larger and much slower to fruit after planting. Rootstocks are used to control the speed of growth and the size of the resulting tree, allowing for easier harvesting.

There are more than 7,500 [[List of apple cultivars|cultivars of apples]]. Different cultivars are bred for various tastes and uses, including [[cooking apple|cooking]], eating raw, and [[apple cider|cider]] production. Trees and fruit are prone to [[fungus|fungal]], bacterial, and pest problems, which can be controlled by a number of [[organic farming|organic]] and non-organic means. In 2010, the fruit's [[genome]] was [[DNA sequencing|sequenced]] as part of research on disease control and selective breeding in apple production.

Worldwide production of apples in 2021 was 93 million [[tonnes]], with China accounting for nearly half of the total.<ref name=faostat/>`;
let wikitext = wtf(article).wikitext();

const parseTemplates = function (wikitext) {
const parseLinks = function (wikitext) {
let wikiLinks = wtf(wikitext).links();

const sisterProjects = {
"wikipedia": "https://en.wikipedia.org/wiki/Wikipedia",
"w": "https://en.wikipedia.org/wiki/Wikipedia",
"wiktionary": "https://en.wiktionary.org/wiki",
"wikt": "https://en.wiktionary.org/wiki",
"wikinews": "https://en.wikinews.org/wiki",
"n": "https://en.wikinews.org/wiki",
"wikibooks": "https://en.wikibooks.org/wiki",
"b": "https://en.wikibooks.org/wiki",
"wikiquote": "https://en.wikiquote.org/wiki",
"q": "https://en.wikiquote.org/wiki",
"wikisource": "https://en.wikisource.org/wiki",
"s": "https://en.wikisource.org/wiki",
"wikispecies": "https://species.wikimedia.org/wiki",
"species": "https://species.wikimedia.org/wiki",
"wikiversity": "https://en.wikiversity.org/wiki",
"v": "https://en.wikiversity.org/wiki",
"wikivoyage": "https://en.wikivoyage.org/wiki",
"voy": "https://en.wikivoyage.org/wiki",
"wikimedia": "https://en.wikipedia.org/wiki/Wikimedia_Foundation",
"foundation": "https://en.wikipedia.org/wiki/Wikimedia_Foundation",
"wmf": "https://en.wikipedia.org/wiki/Wikimedia_Foundation",
"commons": "https://commons.wikimedia.org/wiki",
"c": "https://commons.wikimedia.org/wiki",
"wikidata": "https://www.wikidata.org/wiki",
"d": "https://www.wikidata.org/wiki",
"meta": "https://meta.wikimedia.org/wiki",
"m": "https://meta.wikimedia.org/wiki",
"incubator": "https://en.wikipedia.org/wiki/Wikimedia_Foundation",
"—": "https://en.wikipedia.org/wiki/Wikimedia_Foundation",
"mediawikiwiki": "https://www.mediawiki.org/wiki",
"mw": "https://www.mediawiki.org/wiki",
"phabricator": "https://en.wikipedia.org/wiki/Phabricator",
"phab": "https://en.wikipedia.org/wiki/Phabricator",
}

const sisters = Object.keys(sisterProjects);

let outgoingSlugs = [];

wikiLinks.forEach(ele => {
let replacement = '';

let linkWikiText = ele.wikitext();

if (/^\[\[(.*?)\]\]/g.test(linkWikiText)) {
// internal link
replacement = linkWikiText.replace(/(?<!!)\[\[(.*?)\]\](<nowiki \/>)?(.*?)/, (match, urlText, noWikiText, blendLinkText) => {
let url = "https://en.wikipedia.org/wiki";

return urlText.replace(/([^|\:\|]+)(\:?)([^|\|]*)(\|?)([\w\W]*)/, (match, text, colon, description, line, alias) => {
let slug = text;
let showText = urlText;

if (colon != '') {
if (sisters.includes(text)) {
url = sisterProjects[text.toString().toLowerCase()];
slug = description;
} else {
slug = `${text}:${description}`;
}
}

//Capitalize the first letter of slug since wiki title has first letter capital
slug = slug.charAt(0).toUpperCase() + slug.slice(1);
slug = slug.replace(/\s+/g, '_');

if (alias != '') {
showText = alias;
}

// const internalLink = `[[${showText}${blendLinkText}|${url}/${slug.replace(/ /g, '_')}|${outgoingSlugs.length}]]`;
const internalLink = `[[${slug}|${showText}${blendLinkText}]]`;
outgoingSlugs.push(slug);

return internalLink;
});
}
);
} else {
// external link
replacement = linkWikiText.replace(/(?<!!)\[([^!\s]+)(.*?)\]/gi, (match, url, text) => {
return text ? `[${text}](${url})` : `[${url}](${url})`;
}
)
}

wikitext = wikitext.replace(linkWikiText, replacement);
});
return wikitext;
}

wikitext = parseLinks(wikitext)

let unparsedTemplates = [];
let wikiTemplates = wtf(wikitext).templates();

wikiTemplates.forEach(ele => {
if (ele.text() != '') {
wikitext = wikitext.replace(ele.wikitext(), ele.text());
} else if (ele.wikitext() != '') {
unparsedTemplates.push(ele.wikitext());
}
});
return [wikitext, unparsedTemplates];
}
return parseTemplates(wikitext)
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more