Comparing version 0.6.1 to 0.7.0
@@ -128,2 +128,3 @@ "use strict"; | ||
}; | ||
exports.default = _default; | ||
exports.default = _default; | ||
//# sourceMappingURL=dark-style.js.map |
@@ -6,5 +6,3 @@ "use strict"; | ||
}); | ||
var _tags = require("./tags"); | ||
Object.keys(_tags).forEach(function (key) { | ||
@@ -19,2 +17,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
require("./style.css"); | ||
const target = document.querySelector('#app'); | ||
@@ -13,10 +12,9 @@ if (target == null) throw new Error('app element not found'); | ||
let startTime = null; | ||
function loop(t) { | ||
if (startTime === null) startTime = t; | ||
context.clearRect(0, 0, 1000, 1000); // TODO | ||
context.clearRect(0, 0, 1000, 1000); | ||
// TODO | ||
requestAnimationFrame(loop); | ||
} | ||
requestAnimationFrame(loop); | ||
requestAnimationFrame(loop); | ||
//# sourceMappingURL=main.js.map |
@@ -11,16 +11,11 @@ "use strict"; | ||
exports.ready = ready; | ||
exports.toString = toString; | ||
var _starryNight = require("@wooorm/starry-night"); | ||
var _darkStyle = _interopRequireDefault(require("./dark-style")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const starryNight = (0, _starryNight.createStarryNight)(_starryNight.all); | ||
let starryNightCache = null; | ||
async function ready() { | ||
starryNightCache = await starryNight; | ||
} | ||
const handler = { | ||
@@ -36,17 +31,13 @@ get(_, language) { | ||
} | ||
}; | ||
const language = new Proxy({}, handler); | ||
exports.language = language; | ||
function parse(code) { | ||
const raw = integrate(code); | ||
const raw = integrate(reindent(code)); | ||
if (starryNightCache == null) throw new Error(`you must await ready()`); | ||
const sn = starryNightCache; | ||
const scope = sn.flagToScope(code.language); | ||
if (typeof scope !== 'string') { | ||
throw new Error(`language ${code.language} not found`); | ||
} | ||
const parsed = sn.highlight(raw, scope); | ||
@@ -56,9 +47,8 @@ return parsed.children.map(colorRecurse).flat().map(({ | ||
...rest | ||
}) => ({ ...rest, | ||
}) => ({ | ||
...rest, | ||
color: color === '' ? '#c9d1d9' : color | ||
})); | ||
} | ||
const rules = new Map(Object.entries(_darkStyle.default).map(([k, v]) => [k, new Map(Object.entries(v))])); | ||
function getColor(classList) { | ||
@@ -71,3 +61,2 @@ console.assert(classList.length <= 1, `classList too long`); | ||
} | ||
function colorRecurse(parsed) { | ||
@@ -81,3 +70,2 @@ if (parsed.type === 'text') { | ||
var _parsed$properties; | ||
const className = (_parsed$properties = parsed.properties) === null || _parsed$properties === void 0 ? void 0 : _parsed$properties.className; | ||
@@ -87,3 +75,2 @@ const color = getColor(className ?? []); | ||
const result = []; | ||
const emit = () => { | ||
@@ -102,9 +89,6 @@ if (temp !== '') { | ||
} | ||
temp = ''; | ||
} | ||
}; | ||
let temp = ''; | ||
for (const child of children) { | ||
@@ -119,6 +103,4 @@ for (const item of child) { | ||
} | ||
emit(); | ||
} | ||
emit(); | ||
@@ -130,3 +112,47 @@ return result; | ||
} | ||
function toString(tokens) { | ||
return tokens.map(token => token.code).join(''); | ||
} | ||
function reindent(code, indent = 0) { | ||
var _code$spans$at; | ||
if (typeof code === 'string') { | ||
if (code.at(0) !== '\n') { | ||
return ' '.repeat(indent) + code; | ||
} | ||
const regex = /^ +/gm; | ||
const matches = code.matchAll(regex); | ||
let min = Infinity; | ||
for (const match of matches) min = Math.min(min, match[0].length); | ||
const undent = new RegExp(`^ {${min}}`, 'gm'); | ||
const result = code.substring(1).replace(undent, ' '.repeat(indent)); | ||
return result; | ||
} | ||
if (((_code$spans$at = code.spans.at(0)) === null || _code$spans$at === void 0 ? void 0 : _code$spans$at.at(0)) !== '\n') return code; | ||
const regex = /\n */g; | ||
const matches = code.spans.flatMap(span => [...span.matchAll(regex)]); | ||
let min = Infinity; | ||
for (const [match] of matches) min = Math.min(min, match.length - 1); | ||
const undentRegex = new RegExp(`\n {${min}}`, 'g'); | ||
const spans = code.spans.map(span => span.replace(undentRegex, '\n' + ' '.repeat(indent))); | ||
spans[0] = spans[0].substring(1); | ||
let index = 0; | ||
const nodes = []; | ||
for (const node of code.nodes) { | ||
const before = spans[index]; | ||
const preindentRegex = /\n *$/; | ||
const indentation = before.match(preindentRegex); | ||
if (indentation != null) { | ||
spans[index] = spans[index].replace(preindentRegex, '\n'); | ||
nodes.push(reindent(node, indentation[0].length - 1)); | ||
} else { | ||
nodes.push(node); | ||
} | ||
index++; | ||
} | ||
return { | ||
language: code.language, | ||
spans, | ||
nodes | ||
}; | ||
} | ||
function integrate(code) { | ||
@@ -138,27 +164,16 @@ if (typeof code === 'string') return code; | ||
} | ||
function isLevelEquivilant(one, two) { | ||
// console.info(`checking if nodes are level equivilant`); | ||
if (one === null || two === null) return false; | ||
if (typeof one === 'string' && typeof two !== 'string' || typeof one !== 'string' && typeof two === 'string') { | ||
// console.info(`found nodes to be different types`); | ||
return false; | ||
} | ||
if (typeof one === 'string' && typeof two === 'string') { | ||
// console.info(`found nodes to be strings`); | ||
return one === two; | ||
} | ||
if (typeof one !== 'string' && typeof two !== 'string') { | ||
// console.info(`found nodes to be code nodes`); | ||
if (one.spans.length !== two.spans.length) return false; // console.info(`found nodes to be the same length`); | ||
if (one.spans.length !== two.spans.length) return false; | ||
return one.spans.every((span, i) => span === two.spans[i]); | ||
} | ||
throw new Error('Could not determine equivilance of nodes'); | ||
} | ||
function chars(tokens) { | ||
@@ -175,7 +190,5 @@ return tokens.flatMap(({ | ||
} | ||
function tokens(chars) { | ||
const result = []; | ||
let token = null; | ||
for (const char of chars) { | ||
@@ -196,8 +209,8 @@ if (token == null) { | ||
} | ||
if (token != null) result.push(token); | ||
return result; | ||
} | ||
function diff(start, end) { | ||
start = reindent(start); | ||
end = reindent(end); | ||
const startParsed = chars(parse(start)); | ||
@@ -208,9 +221,6 @@ const endParsed = chars(parse(end)); | ||
const result = []; | ||
function recurse(one, two) { | ||
const progress = (l, r) => { | ||
if (r == null) r = l; // console.info(`progressing ${l} and ${r}`); | ||
if (r == null) r = l; | ||
const startIndex = index; | ||
while (index < startIndex + l.length && index < startParsed.length) { | ||
@@ -222,7 +232,5 @@ result.push({ | ||
}); | ||
index++; // console.info(`now at ${index} in startParsed`); | ||
index++; | ||
} | ||
const endIndex = endex; | ||
while (endex < endIndex + r.length && two !== '' && endex < endParsed.length) { | ||
@@ -236,13 +244,9 @@ if (r !== l) { | ||
} | ||
endex++; // console.info(`now at ${endex} in endParsed`); | ||
endex++; | ||
} | ||
}; | ||
if (isLevelEquivilant(one, two)) { | ||
if (one == null || two == null) { | ||
throw new Error('equivilant nodes should not be null'); | ||
} // console.info(`nodes were found to be level equivilant`); | ||
} | ||
if (typeof one === 'string') { | ||
@@ -252,3 +256,2 @@ progress(one); | ||
if (typeof two === 'string') throw new Error(); | ||
for (let n = 0; n < one.nodes.length; n++) { | ||
@@ -258,7 +261,5 @@ progress(one.spans[n]); | ||
} | ||
progress(one.spans.at(-1) ?? ''); | ||
} | ||
} else { | ||
// console.info(`nodes were NOT found to be level equivilant`); | ||
if (typeof one === 'string' && typeof two === 'string') { | ||
@@ -271,3 +272,2 @@ progress(one, two); | ||
} | ||
if (one != null && typeof one !== 'string') { | ||
@@ -278,6 +278,4 @@ for (let n = 0; n < one.nodes.length; n++) { | ||
} | ||
progress(one.spans.at(-1) ?? '', ''); | ||
} | ||
if (two != null && typeof two !== 'string') { | ||
@@ -288,3 +286,2 @@ for (let n = 0; n < two.nodes.length; n++) { | ||
} | ||
progress('', two.spans.at(-1)); | ||
@@ -294,3 +291,2 @@ } | ||
} | ||
recurse(start, end); | ||
@@ -310,3 +306,2 @@ let [sat, sln, eat, eln] = [0, 0, 0, 0]; | ||
}; | ||
if (code === '\n') { | ||
@@ -317,3 +312,2 @@ if (morph !== 'create') { | ||
} | ||
if (morph !== 'delete') { | ||
@@ -327,6 +321,11 @@ eat = 0; | ||
} | ||
return value; | ||
}); | ||
return tokens(positioned); | ||
} | ||
const tokenized = tokens(positioned); | ||
return tokenized.filter(({ | ||
code | ||
}) => { | ||
return code.length !== 0 && !/^ +$/.test(code); | ||
}); | ||
} | ||
//# sourceMappingURL=tags.js.map |
@@ -121,2 +121,3 @@ export default { | ||
} | ||
}; | ||
}; | ||
//# sourceMappingURL=dark-style.js.map |
@@ -10,10 +10,9 @@ import './style.css'; | ||
let startTime = null; | ||
function loop(t) { | ||
if (startTime === null) startTime = t; | ||
context.clearRect(0, 0, 1000, 1000); // TODO | ||
context.clearRect(0, 0, 1000, 1000); | ||
// TODO | ||
requestAnimationFrame(loop); | ||
} | ||
requestAnimationFrame(loop); | ||
requestAnimationFrame(loop); | ||
//# sourceMappingURL=main.js.map |
@@ -18,15 +18,12 @@ import { createStarryNight, all } from '@wooorm/starry-night'; | ||
} | ||
}; | ||
export const language = new Proxy({}, handler); | ||
export function parse(code) { | ||
const raw = integrate(code); | ||
const raw = integrate(reindent(code)); | ||
if (starryNightCache == null) throw new Error(`you must await ready()`); | ||
const sn = starryNightCache; | ||
const scope = sn.flagToScope(code.language); | ||
if (typeof scope !== 'string') { | ||
throw new Error(`language ${code.language} not found`); | ||
} | ||
const parsed = sn.highlight(raw, scope); | ||
@@ -36,3 +33,4 @@ return parsed.children.map(colorRecurse).flat().map(({ | ||
...rest | ||
}) => ({ ...rest, | ||
}) => ({ | ||
...rest, | ||
color: color === '' ? '#c9d1d9' : color | ||
@@ -49,3 +47,2 @@ })); | ||
} | ||
function colorRecurse(parsed) { | ||
@@ -62,3 +59,2 @@ if (parsed.type === 'text') { | ||
const result = []; | ||
const emit = () => { | ||
@@ -77,9 +73,6 @@ if (temp !== '') { | ||
} | ||
temp = ''; | ||
} | ||
}; | ||
let temp = ''; | ||
for (const child of children) { | ||
@@ -94,6 +87,4 @@ for (const item of child) { | ||
} | ||
emit(); | ||
} | ||
emit(); | ||
@@ -105,3 +96,46 @@ return result; | ||
} | ||
export function toString(tokens) { | ||
return tokens.map(token => token.code).join(''); | ||
} | ||
function reindent(code, indent = 0) { | ||
if (typeof code === 'string') { | ||
if (code.at(0) !== '\n') { | ||
return ' '.repeat(indent) + code; | ||
} | ||
const regex = /^ +/gm; | ||
const matches = code.matchAll(regex); | ||
let min = Infinity; | ||
for (const match of matches) min = Math.min(min, match[0].length); | ||
const undent = new RegExp(`^ {${min}}`, 'gm'); | ||
const result = code.substring(1).replace(undent, ' '.repeat(indent)); | ||
return result; | ||
} | ||
if (code.spans.at(0)?.at(0) !== '\n') return code; | ||
const regex = /\n */g; | ||
const matches = code.spans.flatMap(span => [...span.matchAll(regex)]); | ||
let min = Infinity; | ||
for (const [match] of matches) min = Math.min(min, match.length - 1); | ||
const undentRegex = new RegExp(`\n {${min}}`, 'g'); | ||
const spans = code.spans.map(span => span.replace(undentRegex, '\n' + ' '.repeat(indent))); | ||
spans[0] = spans[0].substring(1); | ||
let index = 0; | ||
const nodes = []; | ||
for (const node of code.nodes) { | ||
const before = spans[index]; | ||
const preindentRegex = /\n *$/; | ||
const indentation = before.match(preindentRegex); | ||
if (indentation != null) { | ||
spans[index] = spans[index].replace(preindentRegex, '\n'); | ||
nodes.push(reindent(node, indentation[0].length - 1)); | ||
} else { | ||
nodes.push(node); | ||
} | ||
index++; | ||
} | ||
return { | ||
language: code.language, | ||
spans, | ||
nodes | ||
}; | ||
} | ||
function integrate(code) { | ||
@@ -113,27 +147,16 @@ if (typeof code === 'string') return code; | ||
} | ||
function isLevelEquivilant(one, two) { | ||
// console.info(`checking if nodes are level equivilant`); | ||
if (one === null || two === null) return false; | ||
if (typeof one === 'string' && typeof two !== 'string' || typeof one !== 'string' && typeof two === 'string') { | ||
// console.info(`found nodes to be different types`); | ||
return false; | ||
} | ||
if (typeof one === 'string' && typeof two === 'string') { | ||
// console.info(`found nodes to be strings`); | ||
return one === two; | ||
} | ||
if (typeof one !== 'string' && typeof two !== 'string') { | ||
// console.info(`found nodes to be code nodes`); | ||
if (one.spans.length !== two.spans.length) return false; // console.info(`found nodes to be the same length`); | ||
if (one.spans.length !== two.spans.length) return false; | ||
return one.spans.every((span, i) => span === two.spans[i]); | ||
} | ||
throw new Error('Could not determine equivilance of nodes'); | ||
} | ||
function chars(tokens) { | ||
@@ -150,7 +173,5 @@ return tokens.flatMap(({ | ||
} | ||
function tokens(chars) { | ||
const result = []; | ||
let token = null; | ||
for (const char of chars) { | ||
@@ -171,8 +192,8 @@ if (token == null) { | ||
} | ||
if (token != null) result.push(token); | ||
return result; | ||
} | ||
export function diff(start, end) { | ||
start = reindent(start); | ||
end = reindent(end); | ||
const startParsed = chars(parse(start)); | ||
@@ -183,9 +204,6 @@ const endParsed = chars(parse(end)); | ||
const result = []; | ||
function recurse(one, two) { | ||
const progress = (l, r) => { | ||
if (r == null) r = l; // console.info(`progressing ${l} and ${r}`); | ||
if (r == null) r = l; | ||
const startIndex = index; | ||
while (index < startIndex + l.length && index < startParsed.length) { | ||
@@ -197,7 +215,5 @@ result.push({ | ||
}); | ||
index++; // console.info(`now at ${index} in startParsed`); | ||
index++; | ||
} | ||
const endIndex = endex; | ||
while (endex < endIndex + r.length && two !== '' && endex < endParsed.length) { | ||
@@ -211,13 +227,9 @@ if (r !== l) { | ||
} | ||
endex++; // console.info(`now at ${endex} in endParsed`); | ||
endex++; | ||
} | ||
}; | ||
if (isLevelEquivilant(one, two)) { | ||
if (one == null || two == null) { | ||
throw new Error('equivilant nodes should not be null'); | ||
} // console.info(`nodes were found to be level equivilant`); | ||
} | ||
if (typeof one === 'string') { | ||
@@ -227,3 +239,2 @@ progress(one); | ||
if (typeof two === 'string') throw new Error(); | ||
for (let n = 0; n < one.nodes.length; n++) { | ||
@@ -233,7 +244,5 @@ progress(one.spans[n]); | ||
} | ||
progress(one.spans.at(-1) ?? ''); | ||
} | ||
} else { | ||
// console.info(`nodes were NOT found to be level equivilant`); | ||
if (typeof one === 'string' && typeof two === 'string') { | ||
@@ -246,3 +255,2 @@ progress(one, two); | ||
} | ||
if (one != null && typeof one !== 'string') { | ||
@@ -253,6 +261,4 @@ for (let n = 0; n < one.nodes.length; n++) { | ||
} | ||
progress(one.spans.at(-1) ?? '', ''); | ||
} | ||
if (two != null && typeof two !== 'string') { | ||
@@ -263,3 +269,2 @@ for (let n = 0; n < two.nodes.length; n++) { | ||
} | ||
progress('', two.spans.at(-1)); | ||
@@ -269,3 +274,2 @@ } | ||
} | ||
recurse(start, end); | ||
@@ -285,3 +289,2 @@ let [sat, sln, eat, eln] = [0, 0, 0, 0]; | ||
}; | ||
if (code === '\n') { | ||
@@ -292,3 +295,2 @@ if (morph !== 'create') { | ||
} | ||
if (morph !== 'delete') { | ||
@@ -302,6 +304,11 @@ eat = 0; | ||
} | ||
return value; | ||
}); | ||
return tokens(positioned); | ||
} | ||
const tokenized = tokens(positioned); | ||
return tokenized.filter(({ | ||
code | ||
}) => { | ||
return code.length !== 0 && !/^ +$/.test(code); | ||
}); | ||
} | ||
//# sourceMappingURL=tags.js.map |
@@ -28,3 +28,4 @@ import type { Root } from 'hast'; | ||
export declare function getColor(classList: string[]): string | undefined; | ||
export declare function toString(tokens: Token[]): string; | ||
export declare function diff(start: CodeTree, end: CodeTree): MorphToken[]; | ||
//# sourceMappingURL=tags.d.ts.map |
{ | ||
"name": "code-fns", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "A library for visualizing code.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
87739
25
1044