Comparing version 0.7.1 to 0.8.0
@@ -128,3 +128,2 @@ "use strict"; | ||
}; | ||
exports.default = _default; | ||
//# sourceMappingURL=dark-style.js.map | ||
exports.default = _default; |
@@ -6,3 +6,5 @@ "use strict"; | ||
}); | ||
var _tags = require("./tags"); | ||
Object.keys(_tags).forEach(function (key) { | ||
@@ -17,3 +19,2 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
}); | ||
//# sourceMappingURL=index.js.map | ||
}); |
"use strict"; | ||
require("./style.css"); | ||
const target = document.querySelector('#app'); | ||
@@ -12,9 +13,10 @@ 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); | ||
//# sourceMappingURL=main.js.map | ||
requestAnimationFrame(loop); |
@@ -12,10 +12,16 @@ "use strict"; | ||
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 = { | ||
@@ -31,6 +37,8 @@ get(_, language) { | ||
} | ||
}; | ||
const language = new Proxy({}, handler); | ||
exports.language = language; | ||
function parse(code) { | ||
function parse(code, options) { | ||
const raw = integrate(reindent(code)); | ||
@@ -40,23 +48,44 @@ if (starryNightCache == null) throw new Error(`you must await ready()`); | ||
const scope = sn.flagToScope(code.language); | ||
if (typeof scope !== 'string') { | ||
throw new Error(`language ${code.language} not found`); | ||
} | ||
const parsed = sn.highlight(raw, scope); | ||
return parsed.children.map(colorRecurse).flat().map(({ | ||
return parsed.children.map(child => colorRecurse(child, (options === null || options === void 0 ? void 0 : options.codeStyle) ?? {})).flat().map(({ | ||
color, | ||
...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) { | ||
const styleMap = new Map([['pl-s', 'stringContent'], ['pl-pds', 'stringPunctuation'], ['pl-c', 'comment'], ['pl-smi', 'variable'], ['pl-v', 'parameter'], ['pl-sr', 'regexpContent'], ['pl-c1', 'literal'], ['pl-k', 'keyword'], ['pl-en', 'entityName']]); | ||
function getColor(classList, codeStyle) { | ||
console.assert(classList.length <= 1, `classList too long`); | ||
const styles = classList.length === 1 ? rules.get(`.${classList[0]}`) : new Map(); | ||
console.assert(((styles === null || styles === void 0 ? void 0 : styles.size) ?? 0) <= 1, `more styles than just color`); | ||
const color = styles === null || styles === void 0 ? void 0 : styles.get('color'); | ||
return color; | ||
if (classList.length === 1) { | ||
const key = styleMap.get(classList[0]); | ||
if (key == null) { | ||
throw new Error(`class name ${classList[0]} not recognized, please report your code to code-fns`); | ||
} | ||
if (codeStyle[key] != null) { | ||
var _codeStyle$key; | ||
return (_codeStyle$key = codeStyle[key]) === null || _codeStyle$key === void 0 ? void 0 : _codeStyle$key.text; | ||
} | ||
const styles = rules.get(`.${classList[0]}`); | ||
console.assert(((styles === null || styles === void 0 ? void 0 : styles.size) ?? 0) <= 1, `more styles than just color`); | ||
return styles === null || styles === void 0 ? void 0 : styles.get('color'); | ||
} | ||
return undefined; | ||
} | ||
function colorRecurse(parsed) { | ||
function colorRecurse(parsed, codeStyle) { | ||
if (parsed.type === 'text') { | ||
@@ -69,6 +98,8 @@ return [{ | ||
var _parsed$properties; | ||
const className = (_parsed$properties = parsed.properties) === null || _parsed$properties === void 0 ? void 0 : _parsed$properties.className; | ||
const color = getColor(className ?? []); | ||
const children = parsed.children.map(colorRecurse); | ||
const color = getColor(className ?? [], codeStyle); | ||
const children = parsed.children.map(child => colorRecurse(child, codeStyle)); | ||
const result = []; | ||
const emit = () => { | ||
@@ -87,6 +118,9 @@ if (temp !== '') { | ||
} | ||
temp = ''; | ||
} | ||
}; | ||
let temp = ''; | ||
for (const child of children) { | ||
@@ -101,4 +135,6 @@ for (const item of child) { | ||
} | ||
emit(); | ||
} | ||
emit(); | ||
@@ -110,7 +146,10 @@ 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') { | ||
@@ -120,6 +159,9 @@ if (code.at(0) !== '\n') { | ||
} | ||
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'); | ||
@@ -129,2 +171,3 @@ const result = code.substring(1).replace(undent, ' '.repeat(indent)); | ||
} | ||
if (((_code$spans$at = code.spans.at(0)) === null || _code$spans$at === void 0 ? void 0 : _code$spans$at.at(0)) !== '\n') return code; | ||
@@ -134,3 +177,5 @@ const regex = /\n */g; | ||
let min = Infinity; | ||
for (const [match] of matches) min = Math.min(min, match.length - 1); | ||
const undentRegex = new RegExp(`\n {${min}}`, 'g'); | ||
@@ -141,2 +186,3 @@ const spans = code.spans.map(span => span.replace(undentRegex, '\n' + ' '.repeat(indent))); | ||
const nodes = []; | ||
for (const node of code.nodes) { | ||
@@ -146,2 +192,3 @@ const before = spans[index]; | ||
const indentation = before.match(preindentRegex); | ||
if (indentation != null) { | ||
@@ -153,4 +200,6 @@ spans[index] = spans[index].replace(preindentRegex, '\n'); | ||
} | ||
index++; | ||
} | ||
return { | ||
@@ -162,2 +211,3 @@ language: code.language, | ||
} | ||
function integrate(code) { | ||
@@ -169,10 +219,14 @@ if (typeof code === 'string') return code; | ||
} | ||
function isLevelEquivilant(one, two) { | ||
if (one === null || two === null) return false; | ||
if (typeof one === 'string' && typeof two !== 'string' || typeof one !== 'string' && typeof two === 'string') { | ||
return false; | ||
} | ||
if (typeof one === 'string' && typeof two === 'string') { | ||
return one === two; | ||
} | ||
if (typeof one !== 'string' && typeof two !== 'string') { | ||
@@ -182,4 +236,6 @@ if (one.spans.length !== two.spans.length) return false; | ||
} | ||
throw new Error('Could not determine equivilance of nodes'); | ||
} | ||
function chars(tokens) { | ||
@@ -196,5 +252,7 @@ return tokens.flatMap(({ | ||
} | ||
function tokens(chars) { | ||
const result = []; | ||
let token = null; | ||
for (const char of chars) { | ||
@@ -215,13 +273,16 @@ if (token == null) { | ||
} | ||
if (token != null) result.push(token); | ||
return result; | ||
} | ||
function diff(start, end) { | ||
function diff(start, end, options) { | ||
start = reindent(start); | ||
end = reindent(end); | ||
const startParsed = chars(parse(start)); | ||
const endParsed = chars(parse(end)); | ||
const startParsed = chars(parse(start, options)); | ||
const endParsed = chars(parse(end, options)); | ||
let index = 0; | ||
let endex = 0; | ||
const result = []; | ||
function recurse(one, two) { | ||
@@ -231,2 +292,3 @@ const progress = (l, r) => { | ||
const startIndex = index; | ||
while (index < startIndex + l.length && index < startParsed.length) { | ||
@@ -240,3 +302,5 @@ result.push({ | ||
} | ||
const endIndex = endex; | ||
while (endex < endIndex + r.length && two !== '' && endex < endParsed.length) { | ||
@@ -250,5 +314,7 @@ if (r !== l) { | ||
} | ||
endex++; | ||
} | ||
}; | ||
if (isLevelEquivilant(one, two)) { | ||
@@ -258,2 +324,3 @@ if (one == null || two == null) { | ||
} | ||
if (typeof one === 'string') { | ||
@@ -263,2 +330,3 @@ progress(one); | ||
if (typeof two === 'string') throw new Error(); | ||
for (let n = 0; n < one.nodes.length; n++) { | ||
@@ -268,2 +336,3 @@ progress(one.spans[n]); | ||
} | ||
progress(one.spans.at(-1) ?? ''); | ||
@@ -279,2 +348,3 @@ } | ||
} | ||
if (one != null && typeof one !== 'string') { | ||
@@ -285,4 +355,6 @@ for (let n = 0; n < one.nodes.length; n++) { | ||
} | ||
progress(one.spans.at(-1) ?? '', ''); | ||
} | ||
if (two != null && typeof two !== 'string') { | ||
@@ -293,2 +365,3 @@ for (let n = 0; n < two.nodes.length; n++) { | ||
} | ||
progress('', two.spans.at(-1)); | ||
@@ -298,2 +371,3 @@ } | ||
} | ||
recurse(start, end); | ||
@@ -313,2 +387,3 @@ let [sat, sln, eat, eln] = [0, 0, 0, 0]; | ||
}; | ||
if (code === '\n') { | ||
@@ -319,2 +394,3 @@ if (morph !== 'create') { | ||
} | ||
if (morph !== 'delete') { | ||
@@ -328,2 +404,3 @@ eat = 0; | ||
} | ||
return value; | ||
@@ -337,3 +414,2 @@ }); | ||
}); | ||
} | ||
//# sourceMappingURL=tags.js.map | ||
} |
@@ -121,3 +121,2 @@ export default { | ||
} | ||
}; | ||
//# sourceMappingURL=dark-style.js.map | ||
}; |
@@ -10,9 +10,10 @@ 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); | ||
//# sourceMappingURL=main.js.map | ||
requestAnimationFrame(loop); |
@@ -18,5 +18,6 @@ import { createStarryNight, all } from '@wooorm/starry-night'; | ||
} | ||
}; | ||
export const language = new Proxy({}, handler); | ||
export function parse(code) { | ||
export function parse(code, options) { | ||
const raw = integrate(reindent(code)); | ||
@@ -26,11 +27,12 @@ if (starryNightCache == null) throw new Error(`you must await ready()`); | ||
const scope = sn.flagToScope(code.language); | ||
if (typeof scope !== 'string') { | ||
throw new Error(`language ${code.language} not found`); | ||
} | ||
const parsed = sn.highlight(raw, scope); | ||
return parsed.children.map(colorRecurse).flat().map(({ | ||
return parsed.children.map(child => colorRecurse(child, options?.codeStyle ?? {})).flat().map(({ | ||
color, | ||
...rest | ||
}) => ({ | ||
...rest, | ||
}) => ({ ...rest, | ||
color: color === '' ? '#c9d1d9' : color | ||
@@ -40,10 +42,26 @@ })); | ||
const rules = new Map(Object.entries(style).map(([k, v]) => [k, new Map(Object.entries(v))])); | ||
export function getColor(classList) { | ||
const styleMap = new Map([['pl-s', 'stringContent'], ['pl-pds', 'stringPunctuation'], ['pl-c', 'comment'], ['pl-smi', 'variable'], ['pl-v', 'parameter'], ['pl-sr', 'regexpContent'], ['pl-c1', 'literal'], ['pl-k', 'keyword'], ['pl-en', 'entityName']]); | ||
export function getColor(classList, codeStyle) { | ||
console.assert(classList.length <= 1, `classList too long`); | ||
const styles = classList.length === 1 ? rules.get(`.${classList[0]}`) : new Map(); | ||
console.assert((styles?.size ?? 0) <= 1, `more styles than just color`); | ||
const color = styles?.get('color'); | ||
return color; | ||
if (classList.length === 1) { | ||
const key = styleMap.get(classList[0]); | ||
if (key == null) { | ||
throw new Error(`class name ${classList[0]} not recognized, please report your code to code-fns`); | ||
} | ||
if (codeStyle[key] != null) { | ||
return codeStyle[key]?.text; | ||
} | ||
const styles = rules.get(`.${classList[0]}`); | ||
console.assert((styles?.size ?? 0) <= 1, `more styles than just color`); | ||
return styles?.get('color'); | ||
} | ||
return undefined; | ||
} | ||
function colorRecurse(parsed) { | ||
function colorRecurse(parsed, codeStyle) { | ||
if (parsed.type === 'text') { | ||
@@ -56,5 +74,6 @@ return [{ | ||
const className = parsed.properties?.className; | ||
const color = getColor(className ?? []); | ||
const children = parsed.children.map(colorRecurse); | ||
const color = getColor(className ?? [], codeStyle); | ||
const children = parsed.children.map(child => colorRecurse(child, codeStyle)); | ||
const result = []; | ||
const emit = () => { | ||
@@ -73,6 +92,9 @@ if (temp !== '') { | ||
} | ||
temp = ''; | ||
} | ||
}; | ||
let temp = ''; | ||
for (const child of children) { | ||
@@ -87,4 +109,6 @@ for (const item of child) { | ||
} | ||
emit(); | ||
} | ||
emit(); | ||
@@ -96,5 +120,7 @@ return result; | ||
} | ||
export function toString(tokens) { | ||
return tokens.map(token => token.code).join(''); | ||
} | ||
function reindent(code, indent = 0) { | ||
@@ -105,6 +131,9 @@ if (typeof code === 'string') { | ||
} | ||
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'); | ||
@@ -114,2 +143,3 @@ const result = code.substring(1).replace(undent, ' '.repeat(indent)); | ||
} | ||
if (code.spans.at(0)?.at(0) !== '\n') return code; | ||
@@ -119,3 +149,5 @@ const regex = /\n */g; | ||
let min = Infinity; | ||
for (const [match] of matches) min = Math.min(min, match.length - 1); | ||
const undentRegex = new RegExp(`\n {${min}}`, 'g'); | ||
@@ -126,2 +158,3 @@ const spans = code.spans.map(span => span.replace(undentRegex, '\n' + ' '.repeat(indent))); | ||
const nodes = []; | ||
for (const node of code.nodes) { | ||
@@ -131,2 +164,3 @@ const before = spans[index]; | ||
const indentation = before.match(preindentRegex); | ||
if (indentation != null) { | ||
@@ -138,4 +172,6 @@ spans[index] = spans[index].replace(preindentRegex, '\n'); | ||
} | ||
index++; | ||
} | ||
return { | ||
@@ -147,2 +183,3 @@ language: code.language, | ||
} | ||
function integrate(code) { | ||
@@ -154,10 +191,14 @@ if (typeof code === 'string') return code; | ||
} | ||
function isLevelEquivilant(one, two) { | ||
if (one === null || two === null) return false; | ||
if (typeof one === 'string' && typeof two !== 'string' || typeof one !== 'string' && typeof two === 'string') { | ||
return false; | ||
} | ||
if (typeof one === 'string' && typeof two === 'string') { | ||
return one === two; | ||
} | ||
if (typeof one !== 'string' && typeof two !== 'string') { | ||
@@ -167,4 +208,6 @@ if (one.spans.length !== two.spans.length) return false; | ||
} | ||
throw new Error('Could not determine equivilance of nodes'); | ||
} | ||
function chars(tokens) { | ||
@@ -181,5 +224,7 @@ return tokens.flatMap(({ | ||
} | ||
function tokens(chars) { | ||
const result = []; | ||
let token = null; | ||
for (const char of chars) { | ||
@@ -200,13 +245,16 @@ if (token == null) { | ||
} | ||
if (token != null) result.push(token); | ||
return result; | ||
} | ||
export function diff(start, end) { | ||
export function diff(start, end, options) { | ||
start = reindent(start); | ||
end = reindent(end); | ||
const startParsed = chars(parse(start)); | ||
const endParsed = chars(parse(end)); | ||
const startParsed = chars(parse(start, options)); | ||
const endParsed = chars(parse(end, options)); | ||
let index = 0; | ||
let endex = 0; | ||
const result = []; | ||
function recurse(one, two) { | ||
@@ -216,2 +264,3 @@ const progress = (l, r) => { | ||
const startIndex = index; | ||
while (index < startIndex + l.length && index < startParsed.length) { | ||
@@ -225,3 +274,5 @@ result.push({ | ||
} | ||
const endIndex = endex; | ||
while (endex < endIndex + r.length && two !== '' && endex < endParsed.length) { | ||
@@ -235,5 +286,7 @@ if (r !== l) { | ||
} | ||
endex++; | ||
} | ||
}; | ||
if (isLevelEquivilant(one, two)) { | ||
@@ -243,2 +296,3 @@ if (one == null || two == null) { | ||
} | ||
if (typeof one === 'string') { | ||
@@ -248,2 +302,3 @@ progress(one); | ||
if (typeof two === 'string') throw new Error(); | ||
for (let n = 0; n < one.nodes.length; n++) { | ||
@@ -253,2 +308,3 @@ progress(one.spans[n]); | ||
} | ||
progress(one.spans.at(-1) ?? ''); | ||
@@ -264,2 +320,3 @@ } | ||
} | ||
if (one != null && typeof one !== 'string') { | ||
@@ -270,4 +327,6 @@ for (let n = 0; n < one.nodes.length; n++) { | ||
} | ||
progress(one.spans.at(-1) ?? '', ''); | ||
} | ||
if (two != null && typeof two !== 'string') { | ||
@@ -278,2 +337,3 @@ for (let n = 0; n < two.nodes.length; n++) { | ||
} | ||
progress('', two.spans.at(-1)); | ||
@@ -283,2 +343,3 @@ } | ||
} | ||
recurse(start, end); | ||
@@ -298,2 +359,3 @@ let [sat, sln, eat, eln] = [0, 0, 0, 0]; | ||
}; | ||
if (code === '\n') { | ||
@@ -304,2 +366,3 @@ if (morph !== 'create') { | ||
} | ||
if (morph !== 'delete') { | ||
@@ -313,2 +376,3 @@ eat = 0; | ||
} | ||
return value; | ||
@@ -322,3 +386,2 @@ }); | ||
}); | ||
} | ||
//# sourceMappingURL=tags.js.map | ||
} |
import type { Root } from 'hast'; | ||
import type { CodeStyle } from './style'; | ||
export interface StarryNight { | ||
@@ -26,6 +27,10 @@ flagToScope: (flag: string) => string | undefined; | ||
} | ||
export declare function parse(code: CodeTree): Token[]; | ||
export declare function getColor(classList: string[]): string | undefined; | ||
export declare function parse(code: CodeTree, options?: { | ||
codeStyle?: CodeStyle; | ||
}): Token[]; | ||
export declare function getColor(classList: string[], codeStyle: CodeStyle): string | undefined; | ||
export declare function toString(tokens: Token[]): string; | ||
export declare function diff(start: CodeTree, end: CodeTree): MorphToken[]; | ||
export declare function diff(start: CodeTree, end: CodeTree, options?: { | ||
codeStyle?: CodeStyle; | ||
}): MorphToken[]; | ||
//# sourceMappingURL=tags.d.ts.map |
{ | ||
"name": "code-fns", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"description": "A library for visualizing code.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
1127
36646
21