Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traits #3280

Merged
merged 52 commits into from
Apr 1, 2022
Merged

Traits #3280

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
6eb208c
hackety hackedly hacked
peterbe Oct 9, 2020
622fe37
tidying up
peterbe Oct 10, 2020
c010972
Merge branch 'master' into traits
peterbe Oct 12, 2020
78c255d
starting to implmenent use of react-table
peterbe Oct 12, 2020
09bc2b5
unconflict
peterbe Oct 15, 2020
56d86ea
the Ever Macro Used table
peterbe Oct 15, 2020
4b5c811
master merged
peterbe Oct 21, 2020
cb0021d
still not sure about react-table
peterbe Oct 21, 2020
5173de8
alasql is great
peterbe Oct 21, 2020
2bc07cb
going for alasql
peterbe Oct 21, 2020
dcfce84
Merge branch 'master' into traits
peterbe Oct 22, 2020
c666c47
some decent improvements
peterbe Oct 22, 2020
4dfc150
Merge branch 'master' into traits
peterbe Oct 26, 2020
d478829
improvements to SQL parser error detection
peterbe Oct 27, 2020
05a7cd5
use react-simple-code-editor
peterbe Oct 27, 2020
cc8c52f
syntax highlight sql code
peterbe Oct 27, 2020
6a734ae
split up and ability to share URL
peterbe Oct 27, 2020
2a199d5
merge
peterbe Oct 27, 2020
2fef660
upmerged
peterbe Oct 29, 2020
d24c51e
hackety hack
peterbe Oct 29, 2020
7c5f9e0
fix and mastermerge
peterbe Nov 2, 2020
49c1dc0
mastermerge
peterbe Nov 18, 2020
ed88bd1
mastermerge
peterbe Dec 7, 2020
829782f
mastermerge
peterbe Dec 22, 2020
8ad56b7
count images
peterbe Dec 22, 2020
5d246fd
count images
peterbe Dec 23, 2020
6ce0ea7
mastermerged
peterbe Jan 7, 2021
d30256a
Merge branch 'master' into traits
peterbe Jan 8, 2021
657d712
ability to search by depth
peterbe Jan 11, 2021
d0cbb51
mastermerged
peterbe Feb 11, 2021
333df89
mastermerged
peterbe Feb 16, 2021
151416f
mastermerged plus inline buttons
peterbe Feb 23, 2021
a4635b0
mastermerged
peterbe Mar 8, 2021
331d74d
upgrade alasql
peterbe Mar 8, 2021
777c9be
improvements
peterbe Mar 11, 2021
43ad289
mainmerged
peterbe Mar 18, 2021
5ca0aff
misc updates
peterbe Mar 18, 2021
212d44c
remove comment
peterbe Mar 18, 2021
ac272a0
Merge branch 'main' into traits
peterbe Mar 22, 2021
7e55958
small fixes
peterbe Mar 22, 2021
e462502
get rid of 'Share query' for now
peterbe Mar 22, 2021
3c01dd0
mastermerge
peterbe Apr 28, 2021
78f71ee
it's rawBody not rawHTML now
peterbe Apr 28, 2021
d611e0c
mainmerged
peterbe May 3, 2021
b2bb87d
use-debounce
peterbe May 3, 2021
8ec72ee
cleaning
peterbe May 3, 2021
344ca3c
a much faster KS parser
peterbe May 3, 2021
3a1f577
unconflicted
peterbe May 12, 2021
f732a21
small fixes
peterbe May 27, 2021
7248297
updated
peterbe Jun 7, 2021
ce40f72
ability to query by the number of pre tags
peterbe Jun 7, 2021
4f038b0
Merge branch 'main' into traits
Apr 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ server/node_modules/
/content/files/en-us/

/deployer
/traits
kumascript/tests
testing/
import/
Expand Down
120 changes: 120 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
execGit,
} = require("../content");
const kumascript = require("../kumascript");
const { normalizeMacroName } = require("../kumascript/src/render.js");

const { FLAW_LEVELS } = require("./constants");
const {
Expand Down Expand Up @@ -668,10 +669,129 @@ function renderContributorsTxt(wikiContributorNames = null, githubURL = null) {
return txt;
}

function* fastKSParser(s, includeArgs = false) {
for (const match of s.matchAll(
/\{\{\s*(\w+[\w-.]*\w+)\s*(\((.*?)\)|)\s*\}\}/gms
)) {
const { index } = match;
if (s.charAt(index - 1) === "\\") {
continue;
}

const split = (match[3] || "").trim().split(",");
const found = { name: match[1] };
if (includeArgs) {
found.args = split
.map((s) => s.trim())
.map((s) => {
if (s.startsWith('"') && s.endsWith('"')) {
return s.slice(1, -1);
}
if (s.startsWith("'") && s.endsWith("'")) {
return s.slice(1, -1);
}
return s;
})
.filter((s, i) => {
if (!s) {
// Only return false if it's NOT first
if (i === 0) {
return Boolean(s);
}
}
return true;
});
}
yield found;
}
}

async function analyzeDocument(document) {
const { metadata } = document;

const doc = {
...metadata,
contributors: metadata.contributors ? metadata.contributors.length : 0,
isArchive: !!document.isArchive,
isTranslated: !!document.isTranslated,
};

doc.normalizedMacrosCount = {};
for (const token of fastKSParser(document.rawBody)) {
const normalizedMacroName = normalizeMacroName(token.name);
if (!(normalizedMacroName in doc.normalizedMacrosCount)) {
doc.normalizedMacrosCount[normalizedMacroName] = 0;
}
doc.normalizedMacrosCount[normalizedMacroName]++;
}
doc.tags = document.metadata.tags || [];

doc.fileSize = fs.statSync(document.fileInfo.path).size;
doc.wordCount = document.rawBody
.replace(/(<([^>]+)>)/g, "")
.split(/\s+/).length;
const $ = cheerio.load(document.rawBody);
const imageCounts = countImages($);
doc.images = imageCounts.total;
doc.externalImages = imageCounts.external;
doc.internalImages = imageCounts.internal;
doc.h2s = $("h2").length;
doc.h3s = $("h3").length;
doc.pres = $("pre").length;
doc.title = metadata.title;
doc.mdn_url = document.url;
doc.depth = document.url.split("/").length - 3;

// If the document has a `.popularity` make sure don't bother with too
// many significant figures on it.
doc.popularity = metadata.popularity
? Number(metadata.popularity.toFixed(4))
: 0.0;

doc.modified = metadata.modified || null;

const otherTranslations = document.translations || [];
if (!otherTranslations.length && metadata.translation_of) {
// If built just-in-time, we won't have a record of all the other translations
// available. But if the current document has a translation_of, we can
// at least use that.
otherTranslations.push({ locale: "en-US", slug: metadata.translation_of });
}

if (otherTranslations.length) {
doc.other_translations = otherTranslations;
}

return doc;
}

function countImages($) {
const counts = {
external: 0,
internal: 0,
total: 0,
};
$("img[src]").each((i, img) => {
const src = $(img).attr("src");
if (
src.includes("://") ||
src.startsWith("/@api/") ||
src.startsWith("/files")
) {
counts.external++;
} else {
counts.internal++;
}
});
counts.total = counts.external + counts.internal;
return counts;
}

module.exports = {
FLAW_LEVELS,

buildDocument,
analyzeDocument,

buildLiveSamplePageFromURL,
renderContributorsTxt,
Expand Down
1 change: 0 additions & 1 deletion client/pwa/src/unpack-cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint no-restricted-globals: ["off", "self"] */
import * as zip from "@zip.js/zip.js";
import { openContentCache } from "./caches";
import { INTERACTIVE_EXAMPLES_URL } from "./service-worker";

zip.configure({
useWebWorkers: false,
Expand Down
9 changes: 9 additions & 0 deletions client/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ContributorSpotlight } from "./contributor-spotlight";
import { Banner, hasActiveBanners } from "./banners";

const AllFlaws = React.lazy(() => import("./flaws"));
const AllTraits = React.lazy(() => import("./traits"));
const Translations = React.lazy(() => import("./translations"));
const DocumentEdit = React.lazy(() => import("./document/forms/edit"));
const DocumentCreate = React.lazy(() => import("./document/forms/create"));
Expand Down Expand Up @@ -167,6 +168,14 @@ export function App(appProps) {
</StandardLayout>
}
/>
<Route
path="/_traits/*"
element={
<StandardLayout>
<AllTraits />
</StandardLayout>
}
/>
<Route
path="/_edit/*"
element={
Expand Down
2 changes: 1 addition & 1 deletion client/src/setupProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (app) {
});
app.use("/api", proxy);
app.use("/users", proxy);
app.use("/_+(flaws|translations|open|document)", proxy);
app.use("/_+(flaws|translations|open|document|traits)", proxy);
// E.g. search-index.json or index.json
app.use("**/*.json", proxy);
// This has to match what we do in server/index.js in the catchall handler
Expand Down
62 changes: 62 additions & 0 deletions client/src/traits/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
button.button-inline-small {
display: inline;
margin-right: 2px;
white-space: nowrap;
padding: 2px 4px;
font-weight: normal;
min-height: 30px;
}
button:disabled {
opacity: 0.6;
}

.all-traits {
.sub-nav {
margin: 100px;
li {
margin: 20px;
}
}
.loading {
text-align: center;
margin: 200px;

progress {
width: 600px;
height: 30px;
border-radius: 2px;
}
}

.all-macros-used {
th.filter {
min-width: 350px;
.filter-macros {
text-align: right;
}
}

td.mdn_url {
font-size: 80%;
text-align: right;
text-overflow: ellipsis;
overflow: hidden;
}
td.count,
th.macro {
max-width: 22px;
font-size: 90%;
}
td.count {
span.zero {
color: #a4a4a4;
}
}
th.macro {
text-align: center;
transform-origin: left;
transform: rotate(-90deg);
vertical-align: bottom;
}
}
}
Loading