Skip to content

Commit

Permalink
better parsing & linking, snippeting
Browse files Browse the repository at this point in the history
  • Loading branch information
colevandersWands committed May 9, 2024
1 parent a3ce298 commit 74a3984
Show file tree
Hide file tree
Showing 66 changed files with 937 additions and 389 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"editor.formatOnSave": false,
"editor.tabSize": 2,
"editor.insertSpaces": false,
"files.autoSave": "onFocusChange",
"editor.fontFamily": "monospace",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"files.autoSave": "onFocusChange",
"files.trimTrailingWhitespace": false
}
48 changes: 24 additions & 24 deletions gather-snippets/dynamic-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,31 @@ export const dynamicTags = [
}
},

function translation(snippets = []) {
const lang = (name = '') => {
const ext = name.split('.').pop().toLowerCase();
return ext === 'mjs' ? 'js' : ext;
};
// function translation(snippets = []) {
// const lang = (name = '') => {
// const ext = name.split('.').pop().toLowerCase();
// return ext === 'mjs' ? 'js' : ext;
// };

const universalize = (name = '') =>
((nameParts) => (nameParts.pop(), nameParts.join('.')))(name.split('.'))
.replaceAll('_', '-')
.toLowerCase();
// const universalize = (name = '') =>
// ((nameParts) => (nameParts.pop(), nameParts.join('.')))(name.split('.'))
// .replaceAll('_', '-')
// .toLowerCase();

for (let i = 0; i < snippets.length; i++) {
const oneSnippet = snippets[i];
if (oneSnippet.title.startsWith('.')) continue;
// for (let i = 0; i < snippets.length; i++) {
// const oneSnippet = snippets[i];
// if (oneSnippet.title.startsWith('.')) continue;

for (let j = i + 1; j < snippets.length; j++) {
const anotherSnippet = snippets[j];
if (
universalize(oneSnippet.title) === universalize(anotherSnippet.title) &&
lang(oneSnippet.title) !== lang(anotherSnippet.title)
) {
oneSnippet.tags.push('translation');
anotherSnippet.tags.push('translation');
}
}
}
},
// for (let j = i + 1; j < snippets.length; j++) {
// const anotherSnippet = snippets[j];
// if (
// universalize(oneSnippet.title) === universalize(anotherSnippet.title) &&
// lang(oneSnippet.title) !== lang(anotherSnippet.title)
// ) {
// oneSnippet.tags.push('translation');
// anotherSnippet.tags.push('translation');
// }
// }
// }
// },
];
42 changes: 31 additions & 11 deletions gather-snippets/interpret/langs/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ import { fromHtml } from 'hast-util-from-html';
import { visit } from 'unist-util-visit';

import { isRelative, staticLabels } from '../utils.js';
import { findForelinks as findJsForelinks } from './js/find-forelinks.js';

import { css } from './css.js';
import { js } from './js/index.js';

export const html = ({ title = '', text = '' }) => {
const forelinks = new Set();
forelinks.add(
...staticLabels({
text,
begin: /(\<\!\-\-)[\s]*see[\s]*:/gi,
end: /(\-\-\>)/gi,
}),
);

const scripts = [];
const styles = [];
try {
const ast = fromHtml(text, { fragment: true });
visit(ast, (node) => {
Expand All @@ -19,18 +31,25 @@ export const html = ({ title = '', text = '' }) => {
if (forelink) forelinks.add(forelink);
}
// find import statements in inline scripts
const scriptText = node.children?.find(
const nodeText = node.children?.find(
(child) => child.type === 'text' && child.value !== '',
)?.value;
if (node.tagName === 'script' && scriptText) {
forelinks.add(
...findJsForelinks({
code: scriptText,
type: node.properties?.type || 'script',
}),
);
if (node.tagName === 'script' && nodeText) {
const title = node.properties?.type === 'module' ? 'script.mjs' : 'script.js';
const jsNippet = js({ title, text: nodeText });
// scripts.push(jsNippet);

forelinks.add(...jsNippet.forelinks);
}
// if there's ever a style sheet

if (node.tagName === 'style' && nodeText) {
const csSnippet = css({ title: 'style.css', text: nodeText });
// styles.push(csSnippet);

forelinks.add(...csSnippet.forelinks);
}

// forelinks to style sheets
if (node.tageName === 'link') null;
// can search for CSS imports if ever
});
Expand All @@ -40,7 +59,6 @@ export const html = ({ title = '', text = '' }) => {

const tags = staticLabels({
text,
label: 'tags',
begin: /(\<\!\-\-)[\s]*tags[\s]*:/gi,
end: /(\-\-\>)/gi,
});
Expand All @@ -52,5 +70,7 @@ export const html = ({ title = '', text = '' }) => {
.sort()
.filter((fl) => fl),
tags,
scripts,
styles,
};
};
20 changes: 10 additions & 10 deletions gather-snippets/interpret/langs/js/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { fromJs } from 'esast-util-from-js';

import { langs } from '../index.js';

import { lang, name, ext, staticLabels } from '../../utils.js';
import { name, ext, staticLabels } from '../../utils.js';

import { findForelinks } from './find-forelinks.js';
import { jsast } from './jsast.js';

export const js = ({ title = '', text = '' }) => {
let alt;
Expand All @@ -18,16 +17,17 @@ export const js = ({ title = '', text = '' }) => {
alt = hashbangLine?.replace('#!', '').trim();
}

const type = lang(title) === 'mjs' ? 'module' : 'script';
let ast = null;
try {
ast = type === 'module' ? fromJs(text, { module: true }) : fromJs(text);
} catch (_) {
// can ignore syntactically incorrect snippets
}
const ast = jsast({ title, text });

const forelinks = findForelinks(ast);

forelinks.push(
...staticLabels({
text,
begin: /(\/\/)[\s]*see[\s]*:/gi,
}),
);

const tags = staticLabels({
text,
label: 'tags',
Expand Down
14 changes: 14 additions & 0 deletions gather-snippets/interpret/langs/js/jsast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { fromJs } from 'esast-util-from-js';

import { lang } from '../../utils.js';

export const jsast = ({ text = '', title = '' }) => {
const type = lang(title) === 'mjs' ? 'module' : 'script';

try {
return type === 'module' ? fromJs(text, { module: true }) : fromJs(text);
} catch (_) {
// can ignore syntactically incorrect snippets
return null;
}
};
7 changes: 6 additions & 1 deletion gather-snippets/interpret/langs/md/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ export const md = ({ title, text }) => {
}
});

// --- search for a simplit snippet ---
if (Array.isArray(parsedFrontmatter.see) && parsedFrontmatter.see.length !== 0) {
forelinks.add(...parsedFrontmatter.see);
delete parsedFrontmatter.see;
}

if (ext(name(title)).replace('.', '') in langs) {
// --- search for a simplit snippet ---
const splitTitle = title.split('.');
splitTitle.push('st', splitTitle[splitTitle.length - 2]);
const subTitle = splitTitle.join('.');
Expand Down
20 changes: 19 additions & 1 deletion gather-snippets/interpret/langs/txt.js
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
export const txt = (obj) => obj;
import { staticLabels } from '../utils.js';

export const txt = ({ text = '' }) => {
const forelinks = [
...staticLabels({
text,
begin: /(\()[\s]*see[\s]*:/gi,
end: /(\))/gi,
}),
];

const tags = staticLabels({
text,
begin: /(\()[\s]*tags[\s]*:/gi,
end: /(\))/gi,
});

return { text, forelinks, tags };
};
13 changes: 1 addition & 12 deletions gather-snippets/interpret/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@ export const lang = (title = '') => title.split('.').pop();
export const ext = (title = '') => '.' + lang(title);
export const name = (title = '') => title.slice(0, title.length - ext(title).length);

// from bing chat
// const fs = require('fs');
// const path = require('path');

// function isSameDirectory(dirPath, filePath) {
// const absoluteDirPath = path.resolve(dirPath);
// const absoluteFilePath = path.resolve(filePath);

// return path.dirname(absoluteFilePath) === absoluteDirPath;
// }

export const staticLabels = ({
text = '',
label = 'tags',
begin = new Regex(`/(\(|\/\/|<!\-\-|\/\*|\#)[\s]*${label}[\s]*:/`, 'gi'),
begin = new RegExp(`/(\(|\/\/|<!\-\-|\/\*|\#)[\s]*(${label})[\s]*:/`, 'gi'),
end = /(|\-\-\>|\*\/|\)|)/gi,
}) =>
Array.from(
Expand Down
11 changes: 8 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@
d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
fill="currentColor"
class="octo-body"
></path></svg
></a>
></path>
</svg>
</a>

<h1>
<a id="graph" target="_blank" aria-label="graph link">Snippetry</a>
<a id="api" target="_blank" aria-label="API link" href="https://snippetry.onrender.com/api/"
<a
id="api"
target="_blank"
aria-label="API link"
href="https://snippetry.onrender.com/api/"
><img src="./public/favicon.ico"
/></a>
</h1>
Expand Down
1 change: 1 addition & 0 deletions lenses.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"--port": 3456,
"-no-open": true,
"study": {
"save": true
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"bundle:css": "esbuild --bundle public/styles.css --outfile=public/bundle/min.css --minify",
"bundle": "npm run bundle:js & npm run bundle:css",
"publish": "npm run gather & npm run bundle",
"host:dev": "npm run build & nodemon ./api.js",
"host": "npm run build & node ./api.js"
"host:dev": "npm run publish & nodemon ./api.js",
"host": "npm run publish & node ./api.js"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion public/bundle/min.css

Large diffs are not rendered by default.

Loading

0 comments on commit 74a3984

Please sign in to comment.